index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import storage from '../common/storage'
  4. Vue.use(Vuex);
  5. const store = new Vuex.Store({
  6. // 属性值
  7. state: {
  8. users: storage.getJson("users"),
  9. mobile:storage.getJson("mobile"),
  10. },
  11. // Mutation 必须是同步函数
  12. // 更改state属性内容
  13. // 使用:this.$store.commit("setUserInfo",{ });
  14. mutations: {
  15. UPDATEUSERS(state, data){
  16. state.users = data;
  17. storage.setJson("users",data);
  18. },
  19. DELETEUSERS(state,name){
  20. state.users = null;
  21. storage.remove(name);
  22. },
  23. UPDATEMOBILE(state, data){
  24. state.mobile = data;
  25. storage.setJson("mobile",data);
  26. },
  27. DELETEMOBILE(state,name){
  28. state.mobile = null;
  29. storage.remove(name);
  30. }
  31. },
  32. // Action 可以包含任意异步操作
  33. // 通过 context.commit 可以方问mutations方法
  34. // 也可以获得getters内容
  35. // 通过 this.$store.dispatch("getUser") 来取得内容
  36. actions: {
  37. getUser(context){
  38. //console.log(context.getters.cart)
  39. },
  40. usersStatus(context){
  41. return new Promise(function (resolve, reject) {
  42. let users = storage.getJson("users");
  43. if(users == null){
  44. reject();
  45. }else{
  46. resolve();
  47. }
  48. });
  49. },
  50. usersMobile(context){
  51. return new Promise(function (resolve, reject) {
  52. let mobile = storage.getJson("mobile");
  53. if(mobile == null){
  54. reject();
  55. }else{
  56. resolve();
  57. }
  58. });
  59. }
  60. }
  61. })
  62. export default store