今天在项目中碰到这样一个问题:
从父组件中传过来的props中的数据,在子组件中想加入一个变量。在created中加入变量,在方法中打印次变量是有的,但是当变量发生变化之后,视图中是响应不到的。
解决此种问题有两种方法:
一、直接操作props中的数据
在computed中写下:
computed:{
newData:{
if(this.oldData.list != undefined){
this.oldData.list.map(item=>{
this.$set(item,'isChecked','');
})
}
}
}复制代码
在一个对象中添加一个新的属性
vm.$set(vm.userProfile, 'age', 27)复制代码
添加多个新属性
vm.userProfile = Object.assign({}, vm.userProfile, {
age: 27,
favoriteColor:'pink'
});复制代码
二、在更改新添加的属性的时候操作
toggleShow(item){
item.isChecked = !item.isChecked;
this.newData.list.reverse().reverse();
}复制代码
所有评论(0)