保存在vuex里的数据,页面刷新就会丢失,可以用vuex-persistedstate来持久化,默认用的是localStorage的存储方式
安装
npm install vuex-persistedstate --save-dev
在store.js里引入
import createPersistedState from 'vuex-persistedstate'
const store = new Vuex.Store({
state: {
username: '',
companyId: ''
},
mutations: {
getUsername(state, payload) {
// sessionStorage.setItem('username', JSON.stringify(payload))
state.username = payload
},
getCompanyId(state, payload) {
state.companyId = payload
},
},
plugins: [
createPersistedState(
{
reducer(state) {//state就是vuex的state对象
return {
companyId: state.companyId,
username111: state.username,//username111:保存的key,s.username:保存的value
}
}
}
),
],
})
网友评论