<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
发布于 2018-08-12 15:34:03
v-if确实支持多个条件-例如,假设您具有:
new Vue({
el: '#app',
data: {
x: 1,
y: 2
}
})您将能够编写如下语句:
<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条件:
<sun-cloud v-if="iconSelect === '04d' || iconSelect === '04n'"></sun-cloud>https://stackoverflow.com/questions/51806564
复制相似问题