1-下载插件
npm install vuex-persistedstate --save
2- 在store内 引入插件
import createPersistedState from "vuex-persistedstate"
3-使用插件
const store = new Vuex.Store({
//使用插件
plugins: [createPersistedState()],
//vuex数据
state:{
// 总商品列表
goodsList:[],
//详情页列表
cartList:[],
},
mutations:{
// 主页点击加入购物车
addGoods(state,val){
state.goodsList= val
// console.log( state.goodsList);
},
// 详情页
addCartGoods(state,val){
state.cartList = val;
},
//删除
del(state,index){
state.goodsList.splice(index,1)
}
}
}
4-说明
数据默认保存到LocalStorage中