<div id="app"><!-- 三元表达式 --><p> 今天天气很 {{ weather ? '炎热' : '寒冷' }}</p><button type="button" @click="changestatus()">点我</button></div><script >var vm = new Vue ({data : {weather: true,},methods:{changestatus(){this.weather = ! this.weather}}});vm.$mount("#app")</script>
其实我们并不需要在模版中使用复杂的逻辑,例如三元表达式
我们应该使用 methods 和 computed属性
<body>
<script type="text/javascript" src="https://unpkg.com/vue/dist/vue.js"></script>
<div id="app">
<!-- 三元表达式 -->
<p> 今天天气很 {{ info }}</p>
<button type="button" @click="changestatus()">点我</button>
</div>
<script >
var vm = new Vue ({
data : {
weather: true,
},
methods:{
changestatus(){
this.weather = ! this.weather
}
},
computed: {
info(){
return this.weather ? '炎热' : '寒冷'
}
}
});
vm.$mount("#app")
</script>
</body>
