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();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM