數(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)所森,刷新時不會丟失
})