问题:
将 “苹果” 变成 “香蕉”,打印数组,发现数值变了,但是页面没有渲染
<body>
<section id="demo">
<ul>
<li v-for="item in aaa">
{{item}}
</li>
</ul>
<button @click="change">将苹果变成香蕉</button>
</section>
<script>
// 定义vue范围及监听的变量
new Vue({
el: '#demo',
data: {
aaa: ['苹果', '橘子', '梨']
},
methods:{
change(){
this.aaa[0] = '香蕉'
console.log(this.aaa)
}
}
})
</script>
解决方案:
将 change 方法里
this.aaa[0] = '香蕉'
改成
this.$set(this.aaa, 0, '香蕉')