电子的Vuex动作调度问题

Vuex action dispatch problem with electron

我有带 vuex 的电子应用程序。 Store 是为带有模块的整个应用程序配置的,我的测试模块 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
    }
  }
}

现在我在组件内部尝试发送更新操作,但没有成功。吸气剂工作正常:

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

那么我应该如何调度 action?

电子插件有问题。我使用来自 github 的 electron-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'
})

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

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

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

import store from '../renderer/store'