【问题标题】:Commit mutation from another file nuxt从另一个文件 nuxt 提交突变
【发布时间】:2019-09-12 13:08:12
【问题描述】:

我如何在 user.js 中调用突变,从 whoToFollow.js 调用 reset ?甚至可能吗?这是我的代码:

async logOut({commit}) {
    this.$cookies.remove('token');
    commit('set_token', null);
    commit('whoToFollow/reset');
    this.$router.push('/sign-in');
},

但它不起作用我收到此错误:

unknown local mutation type: whoToFollow/reset, global type: user/whoToFollow/reset

【问题讨论】:

    标签: vuex nuxt.js


    【解决方案1】:

    可以直接从其他商店调用突变。您只是缺少命名空间模块所需的选项“{root: true}”。

    不过,我建议先在另一个商店中调用一个操作,然后再调用突变以保持对 Vuex 模式的真实性。动作 -> 突变

    async logOut({commit, dispatch}) {
       this.$cookies.remove('token');
       commit('set_token', null);
       // in the reset action you can then call the commit
       dispatch('whoToFollow/reset', payloadHere, { root: true })
       this.$router.push('/sign-in');
    },
    

    我建议您查看 Vuex Api 文档以了解有关此内容的更多信息以及为什么需要“root: true”。 https://vuex.vuejs.org/api/#vuex-store-instance-methods

    【讨论】:

      猜你喜欢
      • 2019-08-17
      • 1970-01-01
      • 2017-01-21
      • 2021-04-22
      • 2015-06-22
      • 2021-04-10
      • 2017-07-16
      • 2014-05-03
      • 2020-12-31
      相关资源
      最近更新 更多