1

我正在开发一个通知系统,我遇到了 Vuexfire 来实时绑定通知集合和表格。到目前为止一切顺利,问题是:我可以同时向用户和组发送通知,那么如果选择了用户和组,就会有重复...如果他们删除重复通知,如何消除'同时发送给用户和组?但是,我当前的解决方案在 98% 的情况下都有效,在用户登录组通知后不会立即显示,除非我刷新或在系统中执行某些操作并返回。

我在每个存储用户或组 ID 的文档中都有一个 userIds 和 groupIds 数组。然后我做了两个绑定,如下图在我的vuex store主文件中。

Vue.use(Vuex)

export const store = new Vuex.Store({
  modules: { auth, groups, clients, users, erp, serviceOrders, general, notifications, productivity, management, setup, calendar},
  plugins: [
    createPersistedState({
      storage: window.sessionStorage,
      paths: ['auth.isAuthenticated', 'auth.userName', 'auth.userGroup', 'auth.user.email', 'auth.user.uid', 'auth.sessionStart', 'general.links', 'serviceOrders']
    })
  ],

  state: {
    newNotificationsUser: [],
    newNotificationsGroup: []

  },
  mutations: {
  ...vuexfireMutations
  },
  actions: {
    getNotificationsToUser: firestoreAction(({ bindFirestoreRef, state }) => {
       bindFirestoreRef('newNotificationsUser', db.collection('notifications')
      .where("userIds", "array-contains", state.auth.user.uid)) 
    }),
    getNotificationsToGroup: firestoreAction(({ bindFirestoreRef, state }) => {
      bindFirestoreRef('newNotificationsGroup', db.collection('notifications')
      .where("groupIds", "array-contains", state.auth.userGroup))
    })
  },
  getters: {

  }
})

我收到通知并将它们存储在 newNotificationsUser 和 newNotificationsGroup 中。

在我的组件中,我有这样的东西:

computed: {
    ...mapState({ uniqueNotifications: function(state){
      return Array.from(new Set (state.newNotificationsUser.concat(state.newNotificationsGroup)
      .map(e => JSON.stringify(e))))
      .map(e => JSON.parse(e))
    }
    }),
   },

methods: {
     ...mapActions( ['getNotificationsToUser', 'getNotificationsToGroup']),

}

mounted(){
    this.getNotificationsToUser()
    this.getNotificationsToGroup()

  }

我连接两个数组,然后删除重复项并将结果显示在 uniqueNotifications 变量中。问题是这样的,在第一次登录时它不会加载组部分,只有在我导航到另一个页面并返回之后,或者收到将填充它的新通知。

我尝试更改 created 和 beforeMount 的挂载钩子,但无济于事。我认为问题出在 concat 方法上......有没有办法在不完全改变它的实现方式的情况下解决这个问题?

只是一些图片来说明我在说什么:登录并重定向到通知组件所在的主页后: 登录并重定向到组件所在的主页后。 没有群组通知,只有用户

然后在刷新或其他任何东西之后它可以工作

在此处输入图像描述

提前致谢!

4

0 回答 0