目录
(1)正常的第一种
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="../vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="root">
<h1>刘家奕{{inFor}}</h1>
<button @click="ljy">切换天气</button>
</div>
<script>
Vue.config.productionTip = false; //阻止 vue在启动时生成生产提示。
const vm = new Vue({
el: "#root",
data: {
is: true,
},
computed: {
inFor() {
return this.is ? "是谁?" : "甘昊的爹";
},
},
methods: {
ljy() {
this.is = !this.is;
},
},
// watch: {
// is: {
// handler(newValue, oldValue) {
// console.log("is发生改变", newValue, oldValue);
// },
// },
// },
});
vm.$watch("is", {
handler(newValue, oldValue) {
console.log("is发生改变", newValue, oldValue);
},
});
</script>
</body>
</html>
注意到这段:
(2)正常的第二种
上面代码已经给出
(3)简写的第一种
vm.$watch("is", function (newValue, oldValue) {
console.log("is发生改变", newValue, oldValue);
});
(4)简写的第二种
watch: {
is(newValue, oldValue) {
console.log("is发生改变", newValue, oldValue);
},
}