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