VUEX如何调用modules里面的mutations

clipboard.png

clipboard.png
我这样调用不行好像,如何调用模块里面的motations呢?

阅读 18.6k
6 个回答

建议定义actions,然后使用mapActions导入,再调用。不建议直接commit。

vm.$store.commit('loginSuccess');

在使用vuex中,我一般是分模块,开启 namespaced;


export default {
  namespaced: true,
  state: {
    options: [],
  },
  mutations: {
    // 添加tabs
    add_tabs(state, data) {
      this.state.options.push(data);
    },
  },
};

如果使用commit调用,如下,也就是说commit参数 tabs/去分割模块;


this.$store.commit('tabs/set_active_index', val);

store 后面不需要加 a。除了 state 是分模块的,其他 mutations 和 actions 都不分模块,因此规划的时候要注意不要重名!

在组件中用this.$store.dispatch('name',{key:val});调用actions里的事件,actions里对应的事件自动调用mutations里的
图片描述

图片描述

图片描述

建议定义Actions

我是这样在非组件中调用 mutations 的:

import store from '@/store/index.js'
store.commit('history/SET_LIST', to)

其中 history/SET_LIST 就是正常的 store/modules/history.js 中的 mutations 方法

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题