數(shù)據(jù)持久化:
刷新頁面但指,vuex里面數(shù)據(jù)丟失鸳谜、清空。有時候我們需要把一些數(shù)據(jù)固話到本地唱捣,即使刷新也不能清空舶胀,例如:登陸狀態(tài)概说、token等。這是就需要用到vuex數(shù)據(jù)持久化
//需要先下載插件
npm install vuex-persistedstate --save 或者 使用
yarn add vuex-persistedstate --save
//在vuex初始化時導(dǎo)入插件
import persist from 'vuex-persistedstate'
//并使用
export default new Vuex.Store({
state: {
},
mutations: {
},
actions: {
},
modules: {
},
plugins: [
new persist({
storage: window.localStorage,
}),
],
//會自動保存狀態(tài)嚣伐,刷新時不會丟失
})