[英]Why can't I access my dynamically inserted attribute in my Object via Vue.js?
I have a data method thats setup like this:我有一个这样设置的数据方法:
data () {
return {
live_count: null,
queue: null,
}
}
In my mounted
method I retrieve data for my queue
attribute.在我的
mounted
方法中,我检索queue
属性的数据。 The data structure of my queue is there are objects inside it, and each object has an array of objects.我的队列的数据结构是里面有对象,每个对象都有一个对象数组。 (look at screenshot below)
(看下面的截图)
Now after my retrieve is completed I periodically check if some timestamps are greater than 1 hour ago and set either true/false on a new attribute like so:现在,在我的检索完成后,我会定期检查某些时间戳是否大于 1 小时前,并在新属性上设置真/假,如下所示:
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)
});
In my Vue.js component when I iterate over my queue
object I can read/diplay the items from the queue fine, eg queue_entry_time_ms
, but when I print out past_grace
nothing is shown.在我的 Vue.js 组件中,当我迭代
queue
对象时,我可以很好地读取/显示队列中的项目,例如queue_entry_time_ms
,但是当我打印出past_grace
时,什么也没有显示。
Here is what my code looks like:这是我的代码的样子:
<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>
In the Vue.js debugger I can see the attribute is set like below:在 Vue.js 调试器中,我可以看到属性设置如下:
Is there something I am doing wrong?有什么我做错了吗?
I have experienced this myself.我自己也经历过。 For my issue, the new attribute passed to the object wasn't responsive because Vue wasn't watching for it to change.
对于我的问题,传递给对象的新属性没有响应,因为 Vue 没有注意它的变化。
My solution was to use Vue.set(), documented here: https://v2.vuejs.org/v2/api/#Vue-set我的解决方案是使用 Vue.set(),记录在这里: https ://v2.vuejs.org/v2/api/#Vue-set
It allows you to pass a new responsive attribute to an object after the object is first declared in data()它允许您在对象首次在 data() 中声明后将新的响应属性传递给对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.