我有Vuex的 Electron 应用程序。使用模块(我的测试模块Browser.js)为整个应用配置了商店:
export default {
namespaced: true,
state: {
currentPath: 'notSet'
},
mutations: {
setPath (state) {
state.currentPath = 'xxxx'
}
},
actions: {
updateCurrentPath ({ commit }) {
console.log('COMMIT')
commit('setPath')
}
},
getters: {
getCurrentPath (state) {
return state.currentPath
}
}
}
现在在组件Im中尝试分发更新操作失败。 setter/getter 工作正常:
mounted () {
console.log('mounted')
console.log('# GET FROM STORE:', this.$store.getters['Browser/getCurrentPath'])
console.log('# DISPATCH:', this.$store.dispatch('Browser/updateCurrentPath'))
console.log('# GET FROM STORE 2:', this.$store.getters['Browser/getCurrentPath'])
},
安慰:
mounted
Browser.vue?31a5:62 # GET FROM STORE: notSet
Browser.vue?31a5:63 # DISPATCH: undefined
Browser.vue?31a5:64 # GET FROM STORE 2: notSet
Action 存在,当我记录this。$ store变量时,我可以看到:
Store {_committing: false, _actions: {…}, _actionSubscribers: Array(0), _mutations: {…}, _wrappedGetters: {…}, …}
_actions:
Browser/updateCurrentPath
那么我应该如何调度行动呢?
最佳答案
问题出在 Electron 插件上。我正在使用来自github的electronic-vue repo ,并且使用了一个插件:
export default new Vuex.Store({
modules,
plugins: [
createPersistedState(),
createSharedMutations()
],
strict: process.env.NODE_ENV !== 'production'
})
createSharedMutations插件是问题所在。注释掉之后,一切正常:
export default new Vuex.Store({
modules,
plugins: [
createPersistedState()
],
strict: process.env.NODE_ENV !== 'production'
})