首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >V-如果多个条件不起作用

V-如果多个条件不起作用
EN

Stack Overflow用户
提问于 2018-08-12 15:08:55
回答 1查看 5.3K关注 0票数 2
代码语言:javascript
复制
<template>
<div>
    <h2>{{weatherData.city}}</h2>
    <h3>{{weatherData.weather}}</h3>
    <rain-cloud v-if="iconSelect==='09d'"/>
    <sun-cloud v-if="iconSelect==='04d'"/>
    <sunshine v-if="iconSelect=='01d'"/>
    <thunder-cloud v-if="iconSelect=='11d'"/>
    <windy-cloud v-if="iconSelect=='50d'"/>
    <snow-cloud v-if="iconSelect=='13d'"/>
    <div class="container">
        <h2>{{weatherData.temperature}}°C</h2>
        <h4>max temperature: {{weatherData.tempMax}}°C</h4>
        <h4>min temperature: {{weatherData.tempMin}}°C</h4>
        <h4>humidity: {{weatherData.humidity}}%</h4>
    </div>
</div>
</template>

computed:{
        iconSelect(){
            if(this.weatherData.icon==="04n" || "04d"){
                this.weatherData.icon="04d"
            }
            else if(this.weatherData.icon==="01d"|| "01n"){
                this.weatherData.icon="01d"
            }
            else if(this.weatherData.icon==="11d"|| "11n"){
                this.weatherData.icon="11d"
            }
            else if(this.weatherData.icon==="50d"|| "50n"){
                this.weatherData.icon="50d"
            }
            else if(this.weatherData.icon==="13d"|| "13n"){
                this.weatherData.icon="13d"
            }
            else if(this.weatherData.icon==="09d"||"09n"||"10d"||"10n"){
                this.weatherData.icon="09d"
            }
            return this.weatherData.icon
 }

每个组件都是SVG动画。我只想呈现一条when语句为真的语句。但是v-if不支持乘法条件。有什么想法吗?每种天气都有图标代码,例如"01d“和"01n”表示白天和晚上天气晴朗。但是我只需要使用一个SVG

EN

回答 1

Stack Overflow用户

发布于 2018-08-12 15:34:03

v-if确实支持多个条件-例如,假设您具有:

代码语言:javascript
复制
new Vue({
  el: '#app',
  data: {
    x: 1,
    y: 2
  }
})

您将能够编写如下语句:

代码语言:javascript
复制
<div v-if="x === 1 || y === 3">
...
</div>

Vue还提供了v-else-if="condition"v-else指令。

您在iconSelect()中的条件也有一个问题。您已经以以下格式编写了条件:if(this.weatherData.icon === "04n" || "04d")

此条件的计算结果始终为true,因为即使当weatherData.icon === "04n"为false (将weatherData.icon设置为其他值)时,也会计算第二个表达式"04d" -并且它的计算结果为"04n",在条件上下文中,它等同于true

为了让这些条件按照您的预期工作,它们应该采用以下格式:

if(this.weatherData.icon === "04n" || this.weatherData.icon === "04d")

或者,在您的<template>中,您需要类似地更改您的v-if条件:

代码语言:javascript
复制
<sun-cloud v-if="iconSelect === '04d' || iconSelect === '04n'"></sun-cloud>
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51806564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档