1,安装npm install vuex --save
2,创建单独的文件夹用于集中管理vuex中的数据(store文件夹跟src文件夹同级)
3.store下index中的代码如下
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
userinfor:{
longitude:"116.211489",//百度地图经纬度
latitude:"39.995907",
},
menus:{ //菜单中的存储
}
},
mutations: {
// 将经纬度放入store
changeLocal(state, localdata){
state.userinfor.longitude = localdata.longitude;
state.userinfor.latitude = localdata.latitude;
}
},
plugins:[
createPersistedState({
storage:window.sessionStorage,
reducer(val){
return {
userinfor:val.userinfor
}
}
}),
]
// this.$store.commit('changeLocal',{longitude:"116.211489",latitude:"39.995907",});//清除缓存
})
export default store
4,在main。js中引入
5,要在实例中使用你书写的js。
6.在需要的地方通过提交mutations更改store中的值。
this.$store.commit(‘changeLocal’,{longitude:“116.211489”,latitude:“39.995907”});
7.获取store中的值
this.$store.state.userinfor