Vue2 中为什么“数组名=[]”就能更新数组?

<body>
<div id="root">
<ul>
<li v-for="(p,index) in persons" :key="index">
{{p}}
</li>
</ul>
</div>
<script>
const vm = new Vue({
el: '#root',
data: {
persons: []
},
mounted() {
this.persons = [];
this.persons[0] = '张三'
this.persons[1] = '李四'
}
});
</script>
</body>

有了“this.persons = [];”这句页面就能显示出张三、李四,注释掉这句就不行,为什么?

第1个回答  2022-06-29
vue的数组更新检测规则:使用数组方法push、pop、shift、unshift、splice、sort、reverse才可以触发视图更新,其他方法修改数组将不会同步视图追问

this.persons = []; 并不属于push、pop、shift、unshift、splice、sort、reverse这七个方法之一

本回答被网友采纳
第2个回答  2022-05-23
vue的数组更新检测规则:
使用数组方法push、pop、shift、unshift、splice、sort、reverse才可以触发视图更新,其他方法修改数组将不会同步视图