0
我有這樣的代碼 -無法更新 「數據」 使用 「計算」 制定者在vuejs(2)
Vue.component('competetion-list', {
template: `<div><p>{{competition}}</p><p>{{totalCompetitions}}</p></div>`,
props: ['values'],
data: function() {
return {totalCompetitions: this.values.length}
},
computed:{
competition:{
get: function(){
return this.values.length;
},
set: function(){
this.totalCompetitions = this.values.length;
}
}
}
})
和輸出我得到的是 -
15
0
所以,當我打電話competition
它確實返回了values
支柱的長度,但它沒有將長度設置爲totalCompetition
並且顯示0是初始值。
但是,我能夠通過watch
屬性更新totalCompetition
。
watch: {
values: function(){
this.totalCompetetions= this.values.length
}
}
現在,我的問題是,爲什麼我不能通過computed
制定者更新data
屬性值,而getter方法可以返回相同?
你認爲你的'set'函數在什麼時候被調用? –