一、安装
执行命令:cnpm i --save vuex-persistedstate
二、引用
在vuex组件库入口文件index.js引入并调用,此时会将state中的四个状态放到本地存储localStorage里面
import createPersistedState from "vuex-persistedstate";
export default new Vuex.Store({
plugins:[
createPersistedState()
],
state: {
cityName: '北京',
cityId: '110100',
cinemaList: [],
isTabbarShow: true
},
})
如果想要选择某个想要的数据,还可以传参,如:
plugins: [createPersistedState({
reducer: (state) => {
return {
cityId: state.cityId,
cityName: state.cityName
}
}
})],