index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. const store = new Vuex.Store({
  5. state: {
  6. //公共的变量,这里的变量不能随便修改,只能通过触发mutations的方法才能改变
  7. tokenInfo: '',
  8. user: {},
  9. appear: {},
  10. power: []
  11. },
  12. mutations: {
  13. //相当于同步的操作
  14. setTokenInfo(state, newSetToken) {
  15. state.tokenInfo = newSetToken // 修改state里的tokenInfo
  16. },
  17. setUser(state, newSetUser) {
  18. state.user = newSetUser
  19. },
  20. setAppear(state,newSetAppear){
  21. state.appear = newSetAppear
  22. },
  23. setPower(state,newsetPower){
  24. state.power = newsetPower
  25. }
  26. },
  27. actions: {
  28. //相当于异步的操作,不能直接改变state的值,只能通过触发mutations的方法才能改变
  29. addTokenInfo(context, newSetToken) {
  30. context.commit('setTokenInfo', newSetToken)
  31. },
  32. addUser(context, newUser) {
  33. context.commit('setUser', newUser)
  34. },
  35. addAppear(context,newSetAppeaar){
  36. context.commit('setAppear',newSetAppeaar)
  37. },
  38. addPower(context,newSetPower){
  39. context.commit('setPower',newSetPower)
  40. }
  41. }
  42. })
  43. export default store