vue中方法中数据已更新,但是视图却没有变化解决方法

今天在项目中碰到这样一个问题:

从父组件中传过来的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();
}
原文地址:https://www.cnblogs.com/florazeng/p/13460505.html