如何强制VUE检测使用foreach循环在异步方法中完成的阵列变化?

发布于 2025-01-22 06:42:07 字数 1910 浏览 0 评论 0原文

我在模板上使用V-For循环,但是即使V-For循环供应所有具有相同数据对象的项目USerCollections,但只有我从获得其长度和项目更新的函数中称为该功能。我正在渲染每个项目上的一个按钮以调用该功能。总而言之,下面的行更新数组,但只有V-For循环中调用该功能的一个项目。它们都以相同的全局数组启动,但是当我在该数组中添加更多项目(从每个项目中)时,就像每个项目都有他们自己的usercollections数组,即使我正在更新“全局” vue数据对象:userCollections ...

 this.userCollections.push({id: doc.id, ...doc.data()})

我的模板:

 <div v-for="(col, index) in userCollections" :id="col.id" :ref="col.id" class="collRow" @click="addToCollection($event, col.id)">             
    <p >{{col.name}} </p>
               
    <p v-if="col.shortDescription">{{col.shortDescription}}</p>
    <button @click=refreshUserColl(userAddress)>refresh</button>
</div>  

如Vue的文档中所述,Vue无法检测到数组的某些更改: https://v2.vuejs.org/gguide/guide/reaectivitive.htmit.html?redirect=true#For-Arrays < /a> 我假设这是我问题的根源,因此我正在尝试使用链接上建议的解决方法。我正在使用foreach循环,并且正在努力获取正确的语法。 我的代码:

async refreshUserColl(userAddress) {
    let vm = this
    let collsRefs = collection(db, "users", userAddress, 'Collections'); // getting data from Google Firestore
    const qSnap = await getDocs(collsRefs);
    this.userCollections = []
    qSnap.docs.forEach((doc) =>{
        console.log(this)
        
        this.set(vm.userCollections.push({id: doc.id, ...doc.data()})) <--- problem
    })
    
}

请注意this.set(vm.usercollections ...),基本上是我直言不讳地试图翻译Vue的文档上的说明vue.set(vm.items,vm.items,indexofitem,newValue,newValue) )

我遇到了这个错误

任何帮助将不胜感激。

I'm using a v-for loop on the template but even though the v-for loop feeds all items with same data object userCollections, only the ones I call the function from get their lengths and items updated. I'm rendering one button on each item to call the function. In summary the line below updates the array BUT only the one item within the v-for loop that calls the function. They all start the with the same global array, but then when I add more items to that array (from within each item), it's like each item has their own userCollections array, even though I'm updating the "global" Vue data object: userCollections...

 this.userCollections.push({id: doc.id, ...doc.data()})

My template:

 <div v-for="(col, index) in userCollections" :id="col.id" :ref="col.id" class="collRow" @click="addToCollection($event, col.id)">             
    <p >{{col.name}} </p>
               
    <p v-if="col.shortDescription">{{col.shortDescription}}</p>
    <button @click=refreshUserColl(userAddress)>refresh</button>
</div>  

As stated in Vue's documetantion, Vue cannot detect some changes to arrays: https://v2.vuejs.org/v2/guide/reactivity.html?redirect=true#For-Arrays
I'm assuming this is the root of my problem, so I'm trying to use a workaround suggested on the link. I'm using a forEach loop and I'm struggling to get the right syntax.
My code:

async refreshUserColl(userAddress) {
    let vm = this
    let collsRefs = collection(db, "users", userAddress, 'Collections'); // getting data from Google Firestore
    const qSnap = await getDocs(collsRefs);
    this.userCollections = []
    qSnap.docs.forEach((doc) =>{
        console.log(this)
        
        this.set(vm.userCollections.push({id: doc.id, ...doc.data()})) <--- problem
    })
    
}

notice the this.SET(vm.userCollections...) which is basically me bluntly trying to translate the instructions on Vue's documentation Vue.set(vm.items, indexOfItem, newValue)

I'm getting this error
enter image description here

Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文