console
<!DOCTYPE html>
<html lang="en">
<head>
<title>watch Option 监控数据</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script type="text/javascript" src="../assets/js/vue.js"></script>
</head>
<body>
<h1>watch Option 监控数据</h1>
<hr>
<div id="app">
<p>温度显示:{{wendu}}°</p>
<p>穿衣建议:{{chuanyi}}</p>
<p><button @click="shenggao">升高温度</button><button @click="jiangdi">降低温度</button></p>
</div>
<script type="text/javascript">
var chuanyiarray = ["T恤短袖", "夹克长裙", "棉衣羽绒服"];
var app = new Vue({
el: '#app',
data: {
wendu: 14,
chuanyi: "夹克长裙"
},
methods: {
shenggao: function () {
this.wendu += 5;
},
jiangdi: function () {
this.wendu -= 5;
}
}
})
//在外部进行watch监控
app.$watch("wendu",function(newVal, oldVal){
if (newVal > 26) {
this.chuanyi = chuanyiarray[0];
} else if (newVal >= 0 && newVal <= 26) {
this.chuanyi = chuanyiarray[1];
} else {
this.chuanyi = chuanyiarray[2]
}
})
</script>
</body>
</html>