首页 > 解决方案 > 带有绑定的VuexFire同步不起作用

问题描述

我目前正在使用 VuexFire 将 Cloud Firestore 绑定到我的 Vuex 状态。我在让它工作时遇到了一些问题,任何帮助将不胜感激。

我目前正在做的事情如下:

Vue.js 文件:

  methods:{
    ...mapActions("comments", ['bindArticleComments']),
 },created(){
    this.bindArticleComments()
  },

动作/评论文件

  export const bindArticleComments = firestoreAction(({ bindFirestoreRef }) => {
    return bindFirestoreRef('articleComments', collectionRef('comments'))
  })

firebase 服务文件

export const collectionRef = (collectionName) => {
  return firestore().collection(collectionName)
}

奇怪的是,我已经在为不同的 Vuex 状态字段执行相同的过程。它似乎在没有问题的情况下工作。有没有人认为我可能做得不好的事情?

标签: google-cloud-firestorevuexvuexfire

解决方案


奇怪的是我让它工作了,虽然我很难理解它是如何工作的以及为什么工作。在我的 Vue js 文件中,我在下载数据和创建后放置了 this.bindArticleComments()。

methods:{
 downloadComments(){
        const { articlesCommentRef } = this.$fb

        articlesCommentRef(this.loadedArticle.id).get()
          .then(querySnapshot => {
              this.setArticleComments(querySnapshot.docs.map((doc) => doc.data()))
              this.bindArticleComments(this.loadedArticle.id)})
          .catch(error => console.log("Error getting documents: ", error))
          .finally(() => {this.$q.loading.hide()})
      }
},
 created(){
    this.bindArticleComments(this.loadedArticle.id)
  },
  mounted(){
        this.downloadComments()
  }

推荐阅读