07vuex筆記 module 03

// App.vue
<template>
  <div id="app">    

  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
export default {
  name: 'app',
  computed: mapState({
    a: state => state.a.count,
    b: state => state.b.subModule.count
  }),
  // methods: mapActions([
  //   'some/nested/module/foo' // this['some/nested/module/foo']()
  // ])
  methods: mapActions('some/nested/module',[
    'foo' // this.foo()
  ])
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

// main.js
import Vue from 'vue'
import Vuex from 'vuex'
import App from './App.vue'

Vue.use(Vuex)
Vue.config.productionTip = false

const moduleA = {
  namespaced: true,
  state: {
    count: 3
  },
  mutations: {
    increment (state) {
      state.count++;
    }
  },
  getters: {
    doubleCount (state) {
      return state.count * 2;
    }
  },
  actions: {
    incrementIfOdd ({ state, commit }) {
      if(state.count % 2 === 1) {
        commit('increment');
      }
    }
  }
};

const moduleB = {
  namespaced: true,
  modules: {
    subModule: {
      state: {

      },
      mutations: {
        login () {}
      },
      getters: {
        login () {}
      },
      actions: {
        login () {}
      }
    }
  },
  state: {
    count: 8
  },
  mutations: {

  },
  getters: {
    someGetter (state, getters, rootState, rootGetters) {
      rootState.count;
      state.count++;

      getters.someOtherGetter;
      rootGetters.someOtherGetter;
    }
  },
  actions: {
    someAction ({ dispatch, commit, getters, rootGetters }) {
      getters.someGetter;
      rootGetters.someGetter;

      dispatch('someOtherAction');
      dispatch('someOther', null, { root: true });

      commit('someMutation');
      commit('someMutation', null, { root: true });

    }
  }
};

const store = new Vuex.Store({
  modules: {
    a: moduleA,
    b: moduleB
  },
  state: {
    count: 2
  },
  mutations: {

  },
  getters: {

  },
  actions: {

  }
});


new Vue({
  render: h => h(App),
  store
}).$mount('#app')

// window.console.log( store.state.a.count );
// // store.commit('increment');
// store.commit('a/increment');
// window.console.log( store.state.a.count );
// // window.console.log( store.state.b.count );

// store.commit('login');
// store.dispatch('login');
// store.getters.login;

// store.commit('b/login');
// store.dispatch('b/login');
// store.getters['b/login'];

store.commit('b/subModule/login');
store.dispatch('b/subModule/login');
store.getters['b/subModule/login'];

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章