import Vue from 'vue'
import Vuex from 'vuex'
import actions from './actions'
import CreatePersistedState from 'vuex-persistedstate'
Vue.use(Vuex)
const state = {
// loading
showHttpLoading: false,
// 用户code
userCode: '',
// unionId
unionId: ''
}
const mutations = {
// loading
UPDATE_SHOW_HTTP_LOADING (_state, val) {
_state.showHttpLoading = val
},
// 保存 code
SAVE_USER_CODE (_state, val) {
_state.userCode = val
},
// 保存 unionId
SAVE_USER_UNIONID (_state, val) {
_state.unionId = val
}
}
// vuex-persistedstate默认持久化所有state 若不需要全部则把需要持久化的数据放到reducer里 比如 showHttpLoading、unionId 需要持久化 userCode不需要则不需要加入
const vuexPersisted = new CreatePersistedState({
key: 'VuexPersisted',
storage: window.sessionStorage, # // 可选择 localStorage、sessionStorage、cookie 看需求
reducer: state => ({
// loading
showHttpLoading: state.showHttpLoading,
// unionId
unionId: state.unionId,
})
})
// 向外暴露store对象
export default new Vuex.Store({
state,
mutations,
actions,
plugins: [vuexPersisted]
})
import CreatePersistedState from 'vuex-persistedstate'
import createLogger from 'vuex/dist/logger'
export default new Vuex.Store({
state,
mutations,
actions,
plugins: [createLogger(), createPersisted]
})