watch
簡單監視 單個屬性 簡單數據結構
深度監視 複雜屬性 複雜數據結構
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<input type="text" v-model="msg"/>
<button @click="stus[0].name='rose'">change</button>
<h3>{{ msg }}</h3>
<h4>{{ stus[0].name }}</h4>
</div>
<script type="text/javascript" src="./node_modules/vue/dist/vue.js"></script>
<script type="text/javascript">
new Vue({
el: "#app",
data() {
return {
msg: '',
stus: [{name: 'jack'}]
}
},
watch: {
msg: function(newV, oldV) {
console.log(newV, oldV);
if (newV == 'k') {
console.log('lai');
}
},
stus: {
deep: true,
handler: function(newV, oldV) {
console.log(newV[0].name);
}
}
},
})
</script>
</body>
</html>