vue中watch的deep属性深度监听数组对象
程序员文章站
2022-03-29 16:43:34
...
<template>
<div id="app">
<ul>
<li v-for="item in list" :v-key="item.id">{{item.name}}</li>
</ul>
<div>
<input type="text" v-model="value" />
<button @click="add()">添加</button>
<button @click="change()">改变</button>
</div>
</div>
</template>
<script>
export default {
name: 'App',
components: {},
data() {
return {
list: [{
id: 1,
name: '张三'
},
{
id: 2,
name: '李四',
ceshi:0,
list:[
{
a:0
}
]
}
],
value: '45'
}
},
created() {
},
mounted() {
},
watch: {
list: {
handler(newName, oldName) {
console.log('触发监听',this.list,this.list[1].list[0].a);
},
// 开启深度监听
deep: true
}
},
methods: {
add() {
console.log(this.value)
const listItem = {
id: this.list.length,
name: this.value
}
this.list.push(listItem)
},
change() {
// this.list[1].name = this.list[1].name === "赵四" ? '李四' : '赵四'
this.list[1].list[0].a=this.list[1].list[0].a=== 0 ? 1 : 0
}
}
}
</script>
下一篇: 怎样实现webpack4 css打包压缩