VueX-在不同于动作的模块中分配动作

时间:2019-01-26 11:56:30

标签: vue.js vuejs2 vuex

我想在logout上清除用户数据,因此从auth模块我dispatch进行了一个在模块ride中定义的操作,但出现错误:

[vuex] unknown local action type: clearUserData, global type: auth/clearUserData

这是我的代码:

store / modules / auth.js

export const namespaced = true

export const mutations = {
  clearAuthData(state){
    state.apiToken = null
    state.signedIn = false
  }
}

export const actions = {
  logout({ commit, dispatch }) {
    commit('clearAuthData'))
    dispatch('ride/clearUserData', { root: true })

  }
}

store / modules / ride.js

export const namespaced = true

export const state = {
  data: []
}

export const mutations = {
  CLEAR_USER_DATA(state){
    state.data = []
  }
}

export const actions = {  
  clearUserData({ commit }) {
    commit('CLEAR_USER_DATA')
  }
}

store / store.js

import * as auth from './modules/auth'
import * as ride from './modules/ride'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    loading: false
  },
  modules: {
    auth,
    ride
  }
})

我在做什么错了?

2 个答案:

答案 0 :(得分:3)

您的usage of dispatch()错误地传递了 m1.insert(pair<char,string>('A',"def")); m1.insert(pair<char,string>('B',"ghi")); m1.insert(pair<char,string>('C',"jkl")); 作为 2nd 参数,该参数专用于有效负载。选项应为第3个:

{ root: true }

答案 1 :(得分:1)

如果没有要传递的有效负载,则需要将第二个参数作为null发送,如下所示:

dispatch('ride/clearUserData', null, { root: true })