vuex如何实现数据持久化,刷新页面存储的值还存在
1.安装:
npm install vuex-persistedstate --save
2.找到store/index.js
import Vue from 'vue' import Vuex from 'vuex' import VuexPersistense from 'vuex-persistedstate' Vue.use(vuex)
import app from './modules/app' import getters from './getters'
const vuexLocal = new VuexPersistense({ storage: localStorage, reducer: val => { return { //引入app模板,对象里面可配置需要持久化的status app: { sidebar: val.app.sidebar, device: val.app.device } } } }) const store = new Vuex.Store({ modules: { app }, getters, plugins: [vuexLocal] }) export default store