电子的Vuex动作调度问题

时间:2018-12-12 17:10:04

标签: javascript vue.js electron vuex vuex-modules

我有带有vuex的电子应用。使用模块(我的测试模块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内部正在尝试分派更新操作,但没有成功。吸气剂工作正常:

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

存在动作,当我登录this。$ store变量时,我可以看到:

Store {_committing: false, _actions: {…}, _actionSubscribers: Array(0), _mutations: {…}, _wrappedGetters: {…}, …}
_actions:
Browser/updateCurrentPath

那我应该如何调度行动?

3 个答案:

答案 0 :(得分:5)

问题在于电子插件。我使用的是来自github的electronic-vue仓库,并且使用了一个插件:

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'
})

答案 1 :(得分:0)

如果启用了createSharedMutations()插件,则需要在主流程中创建一个store实例。为此,只需将此行添加到您的主进程中(例如src / main.js):

import './path/to/your/store' link to official plug used by electron-vue which is causing this issue

答案 2 :(得分:0)

如果将vue-electron模板与vuex-electron插件一起使用,则需要在src / main / index.js文件中添加以下行

import store from '../renderer/store'