亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我不能通過 Vue.js 訪問對象中動態插入的屬性?

為什么我不能通過 Vue.js 訪問對象中動態插入的屬性?

蝴蝶刀刀 2023-05-25 16:53:40
我有一個這樣設置的數據方法:data () {     return {          live_count: null,          queue: null,     }}在我的mounted方法中,我為我的屬性檢索數據queue。我隊列的數據結構是里面有對象,每個對象都有一個對象數組。(看下面的截圖)現在,在我的檢索完成后,我會定期檢查一些時間戳是否大于 1 小時前,并在新屬性上設置 true/false,如下所示:axios.get(`/my-queue`)    .then(response => {        this.queue = response.data.queue    })    .catch(error => {        console.log(error)        this.errored = true    })    .finally(() => {        this.loading = false;        this._timer = setInterval(() => {            console.log('check queue rows');            const anHourAgo = Date.now() - (60 * 60 * 1000)            for (const [lane_id, items] of Object.entries(this.queue)) {                console.log('lane id: ' + lane_id);                for(const item of items) {                    item.past_grace = item.queue_entry_time_ms > anHourAgo                }            }        }, 1000)    });在我的 Vue.js 組件中,當我遍歷我的queue對象時,我可以很好地讀取/顯示隊列中的項目,例如queue_entry_time_ms,但是當我打印出來時,past_grace什么也沒有顯示。這是我的代碼的樣子:<template v-for="(lane, lane_id) in queue">    <template v-for="(item, index) in lane">        {{ item.queue_entry_time_ms }} <!-- this prints fine -->        {{ item.past_grace }} <!-- this doesnt -->    </template></template>在 Vue.js 調試器中,我可以看到屬性設置如下:我做錯了什么嗎?
查看完整描述

1 回答

?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

我親身經歷過這一點。對于我的問題,傳遞給對象的新屬性沒有響應,因為 Vue 沒有觀察它的變化。

我的解決方案是使用 Vue.set(),它允許您在對象首次在 data() 中聲明后將新的響應屬性傳遞給對象


查看完整回答
反對 回復 2023-05-25
  • 1 回答
  • 0 關注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號