vuex+localStorage vuex刷新失效?

想必大家在使用vuex的时候,vuex刷新后数据失效的问题会困扰大家,本文教大家如何利用本地缓存保存状态

Install

npm install vuex-along --save


1.import 导入
import vuexAlong from 'vuex-along'


  2.add to store
添加至store的 plugins 的数组里
export default new Vuex.Store({
  state:{...},
  ...
  plugins: [vuexAlong]
});


Default save all state
默认保存所有内容
If you don`t want to save all state. Use watch()
只保存部分内容可以使用 watch 方法
vuexAlong.watch(arry,boolean)
arry: attribute or module name list
第一个参数是 属性名或模块名 的数组
boolean (non-essential):  Default true
第二个参数不是必须的 默认true
true = save arry
true 会把 arry 作为要保存的列表
false = filter arry
false 会把 arry 作过滤的列表
If you need clean save. Use clean()
想要清除 localstorage 调用 clean 方法
vuexAlong.clean()




Demo
import Vuex from 'vuex'
import Vue from 'vue'
import vuexAlong from 'vuex-along'


Vue.use(Vuex);


//filter title & num
//此处为保存时过滤 title 和 num
vuexAlong.watch(['title','num'],false);
const state = {
  num: 0,
  title: 'hello',
  name: 'boen',
  people: {
    men: 10,
    women: 10
  }
};
const mutations = {
  addNum(state){
    state.num ++;
  }
};
export default new Vuex.Store({
  state:state,
  mutations:mutations,
  //add vuexAlong
  plugins: [vuexAlong]
});
 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值