uniapp项目中,在原有数据中增加选中的状态,选中后打印的数据显示有变化,然而文本的数据并没有发生变化
看代码
export default {
data() {
return {
thicate: [
{ id: 1, text: "Item 1" },
{ id: 2, text: "Item 2" },
{ id: 3, text: "Item 3" }
],
thicate: []
};
},
methods :{
selectSure(index){
let _this = this;
_this.thicate[index].select = !_this.thicate[index].select
this.thicate = [...this.thicate] //主要加这段代码
console.log(_this.thicate)
}
}
};
在选择数据的时候,需要加上 this.thicate = [...this.thicate];因为在Vue中直接修改数组中的元素(例如使用循环遍历并修改每个元素)不会触发 Vue 的响应式机制,需要重新将数组赋值才能改变状态