刷新页面,vuex里面数据丢失清空。有时候我们需要把一些数据固话到本地,即使刷新也不能清空,例如:登陆状态、token等。这是就需要用到vuex数据持久化
1.下载插件
//需要先下载插件
npm install vuex-persistedstate --save
2.引入 使用
//在vuex初始化时导入插件
import persist from 'vuex-persistedstate'
//并使用
export default new Vuex.Store({
state: {
},
mutations: {
},
actions: {
},
modules: {
},
plugins: [
new persist({
storage: window.localStorage,
}),
],
//会自动保存状态,刷新时不会丢失
})