简体   繁体   English

Vue v-for 不直接更新

[英]Vue v-for doesn't update directly

SOLVED: My bad.The problem is in my code logic, the tempLatest array is always empty so the array that contains the card never been updated.已解决:我的错误。问题出在我的代码逻辑中,tempLatest 数组始终为空,因此包含卡的数组从未更新过。 btw, thank you for all your answers顺便说一句,谢谢你的所有回答

I want to create a program that show a collection of card.我想创建一个显示卡片集合的程序。 That collection will be updated every 15 seconds.该集合将每 15 秒更新一次。 But here comes the problem, the v-for doesn't update directly when a new card added to the list but when the page is refreshed it shows the proper output.但是问题来了,当新卡添加到列表时 v-for 不会直接更新,但是当页面刷新时它会显示正确的输出。

here is my code这是我的代码

<PersonCard v-for="item in latest" v-bind:key="item.id" :attendeeName="item.name" :image="item.picture" :department="item.department"/>
data() {
        return {
            latest: [],
            imgArr: [],
            notReady: true,
        }
    }
created() {
        console.log(this.site, this.sinceTime)
        this.fetchRecognizedFace();
        this.timer = setInterval(this.fetchRecognizedFace, 15000);
    }
// update this.latest
for (var j = 0; j < tempLatest.length; j++) {
    this.latest.unshift(tempLatest[j]);
}

use the vue forceUpdate method ways to rerender component使用 vue forceUpdate 方法重新渲染组件

import Vue from 'vue';
Vue.forceUpdate();

after you update the latest variable, call this one更新最新变量后,调用此变量

this.$forceUpdate();

There are limitations with how arrays can be re-active in Vue (See: https://v2.vuejs.org/v2/guide/list.html#Caveats ).在 Vue 中如何重新激活数组存在一些限制(参见: https ://v2.vuejs.org/v2/guide/list.html#Caveats)。

With this in mind, when you do your update, do it like so:考虑到这一点,当您进行更新时,请这样做:

for (var j = 0; j < tempLatest.length; j++) {
    this.$set(this.latest, j, tempLatest[j]);
}

Note: I've not tested this with your code so you might need to play around a little as you're currently looping the temp list and the indexes might not match but $set is the way to go.注意:我没有使用您的代码对此进行测试,因此您可能需要稍微玩一下,因为您当前正在循环临时列表并且索引可能不匹配,但$set是要走的路。 You might also try:你也可以试试:

this.$set(this, 'latest', tempLatest)

instead of looping each item.而不是循环每个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM