- 安装 vuex-persist
yarn add vuex-persist
- 应用 store.js
import Vue from 'vue'
import Vuex from 'vuex'
import VuexPersistence from 'vuex-persist'
Vue.use(Vuex)
const vuexLocal = new VuexPersistence({
key: 'state', // The key to store the state on in the storage provider.
storage: window.localStorage, // or window.sessionStorage or localForage
})
export default new Vuex.Store({
state: {
user: {},
},
getters: {
user: state => state.user
},
actions: {
},
modules: {
},
plugins: [vuexLocal.plugin]
})
- main.js 引入store.js
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')