一、問題描述
在electron-vue中使用vuex,調用this.$store.dispatch("changeLogin");
時,無法修改登錄狀態,調試了很久都沒有看出問題在哪邊。
store代碼
const state = {
isLogin: false
}
const getters = {
isLogin(state) {
return state.isLogin
}
}
const mutations = {
changeLogin(state) {
state.isLogin = true
}
}
const actions = {
changeLogin(context) {
context.commit("changeLogin")
}
}
export default {
state,
getters,
mutations,
actions
}
調用修改狀態的方法
console.log(this.$store.getters.isLogin);
console.log(this.$store);
this.$store.dispatch("changeLogin");
console.log(this.$store.getters.isLogin);
輸出結果
isLogin
初始是false,調用this.$store.dispatch("changeLogin");
后輸出結果還是false
。
控制臺輸出結果.png
二、解決方法
在GitHub的issue里搜了下,沒有發現類似的問題,最后實在沒辦法,我就在segmentfault上提了這個問題,問題鏈接。
得到的回答是 src/renderer/store/index.js
中引用了createSharedMutations
插件,在注釋掉這個插件后,果然可以改變狀態了。
import Vue from 'vue'
import Vuex from 'vuex'
import { createPersistedState, createSharedMutations } from 'vuex-electron'
import modules from './modules'
Vue.use(Vuex)
export default new Vuex.Store({
modules,
plugins: [
createPersistedState(),
// 注釋這個插件的調用
//createSharedMutations()
],
strict: process.env.NODE_ENV !== 'production'
})
問題是解決了,但還是不了解引起這個問題具體的原因,這個插件是用于多窗口共享狀態的,去掉后應該會影響多窗口的傳值,或許有其他的解決方案。
問題留待繼續跟蹤