import Vue from 'vue' import Vuex from 'vuex' import storage from '../common/storage' Vue.use(Vuex); const store = new Vuex.Store({ // 属性值 state: { users: storage.getJson("users"), mobile:storage.getJson("mobile"), }, // Mutation 必须是同步函数 // 更改state属性内容 // 使用:this.$store.commit("setUserInfo",{ }); mutations: { UPDATEUSERS(state, data){ state.users = data; storage.setJson("users",data); }, DELETEUSERS(state,name){ state.users = null; storage.remove(name); }, UPDATEMOBILE(state, data){ state.mobile = data; storage.setJson("mobile",data); }, DELETEMOBILE(state,name){ state.mobile = null; storage.remove(name); } }, // Action 可以包含任意异步操作 // 通过 context.commit 可以方问mutations方法 // 也可以获得getters内容 // 通过 this.$store.dispatch("getUser") 来取得内容 actions: { getUser(context){ //console.log(context.getters.cart) }, usersStatus(context){ return new Promise(function (resolve, reject) { let users = storage.getJson("users"); if(users == null){ reject(); }else{ resolve(); } }); }, usersMobile(context){ return new Promise(function (resolve, reject) { let mobile = storage.getJson("mobile"); if(mobile == null){ reject(); }else{ resolve(); } }); } } }) export default store