监视属性
通过watch属性可以监视指定变量的变化
<body>
<div id="app">
<div v-bind:title="message">{{message}}</div>
</div>
<script>
var data = {
message:"hello vue"
};
var vue = new Vue({
el:"#app",
data:data,
//监听属性
watch:{
"message":function(newValue, oldValue) {
console.log("newValue = " + newValue + ", oldValue = " + oldValue);
}
}
});
setTimeout(function() {
vue.message = 'update';
}, 5000);
</script>
</body>
还可以通过vue.$watch来监视变量
<body>
<div id="app">
<div v-bind:title="message">{{message}}</div>
</div>
<script>
var data = {
message:"hello vue"
};
var vue = new Vue({
el:"#app",
data:data
//监听属性
// watch:{
// "message":function(newValue, oldValue) {
// console.log("newValue = " + newValue + ", oldValue = " + oldValue);
// }
// }
});
//也可以通过vue.$watch
vue.$watch("message", function(newValue, oldValue) {
console.log("newValue = " + newValue + ", oldValue = " + oldValue);
});
setTimeout(function() {
vue.message = 'update';
}, 5000);
</script>
</body>