基本介绍

创建store

  1. import Vuex from 'vuex'
  2. const store = new Vuex.Store({
  3. state: {
  4. name: '',
  5. obj: '',
  6. age: 18
  7. },
  8. getters: {
  9. fakeAge: (state, getters) => {
  10. return state.age + 2;
  11. }
  12. },
  13. mutations: {
  14. grow(state) { state.age++; },
  15. changeLoading(state, payload) { state.loading = payload.loading; }
  16. },
  17. actions: {
  18. getNickName(context) {
  19. context.commit('changeLoading', { loading: true });
  20. // or
  21. // context.commit({ type: 'changeLoading', payload: { loading: true } });
  22. }
  23. },
  24. });