eletron中使用vuex不生效问题

在基于electron开发时使用vuexdispatch方法传值无效

1
vue.$store.dispatch(VUEX_ACTION_UPDATE_INFO, info)

并且不报任何错误.经查找是vuex-electron的问题.

解决方法1:

store/index.js中去掉createSharedMutations

1
2
3
4
5
6
7
8
export default new Vuex.Store({
modules,
plugins: [
createPersistedState(),
createSharedMutations() // 注释掉这一行
],
strict: process.env.NODE_ENV !== 'production'
})

这是因为 vuex-electron 引入了一个用于多进程间共享 Vuex Store 的状态的插件.如果没有多进程交互的需求,完全可以不引入这个插件.

解决方法2

https://github.com/vue-electron/vuex-electron#installation

看第 3 条:

In case if you enabled createSharedMutations() plugin you need to create an instance of store in the main process. To do it just add this line into your main process (for example src/main.js):

1
2
> import './path/to/your/store'
>

这种时候就不能用第一种方法来解决问题了。

找到 /src/main/index.js,在前面加上一句:

1
import '../renderer/store'
如果您觉得对您有帮助,谢谢您的赞赏!