vuex无法提交子模块mutations

新手上路,请多包涵

vuex模块化之后如何提交子模块的方法?

版本

  • vue3
  • vuex4.x
  • javascript

代码

user.js

export default createStore({
  state: () => ({
    username: 'nihao',
    password: ''
  }),
  mutations: {
    setUsername: (state, username) => {
      state.username = username
    }
  },
  actions: {
  },
  modules: {
  }
})

index.js

import user from './modules/user'
export default createStore({
  state: {
  },
  mutations: {
  },
  actions: {
  },
  getters: {
  },
  modules: {
    user: user
  }
})

调用的方法

const store = useStore()
store.commit('setUsername', 'xxx')
store.commit('user/setUsername', 'xxx')

无论是加了namespaced: true还是不加,上面的两句话都不行。

阅读 1.8k
2 个回答

子模块里面不需要createStore,user.js修改如下:

export default {
  state: () => ({
    username: 'nihao',
    password: ''
  }),
  mutations: {
    setUsername: (state, username) => {
      state.username = username
    }
  },
  actions: {
  }
}
推荐问题
宣传栏