今天在項目中碰到這樣一個問題:
從父組件中傳過來的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(); }