Vuex模塊:開啓命名空間

 

模塊開啓命名空間後,享有獨自的命名空間。css

 

{
    "模塊1":{
        state:{},
        getters:{},
        mutations:{},
        actions:{}
    },
    "模塊2":{
        state:{},
        getters:{},
        mutations:{},
        actions:{}
    }
}

 

mapState、mapGetters、mapMutations、mapActions第一個參數是字符串(命名空間名稱),第二個參數是數組(不須要重命名)/對象(須要重命名)。vue

 

mapXXXs('命名空間名稱',['屬性名1','屬性名2'])

mapXXXs('命名空間名稱',{

  '組件中的新名稱1':'Vuex中的原名稱1',

  '組件中的新名稱2':'Vuex中的原名稱2',

})

 

一 項目結構vuex

 

 

二 main.js數組

 

import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store/index";

Vue.config.productionTip = false;

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

 

三 index.jsapp

 

import Vue from "vue";
import Vuex from "vuex";
import cat from "./modules/cat";
import dog from "./modules/dog";

Vue.use(Vuex);

export default new Vuex.Store({
  modules: { cat, dog }
});

 

四 cat.jsthis

 

export default {
  namespaced: true,
  // 局部狀態
  state: {
    name: "藍白英短",
    age: 1
  },
  // 局部讀取
  getters: {
    desc: state => "寵物:" + state.name
  },
  // 局部變化
  mutations: {
    increment(state, payload) {
      state.age += payload.num;
    }
  },
  // 局部動做
  actions: {
    grow(context, payload) {
      setTimeout(() => {
        context.commit("increment", payload);
      }, 1000);
    }
  }
};

 

五 dog.jsspa

 

export default {
  namespaced: true,
  // 局部狀態
  state: {
    name: "拉布拉多",
    age: 1
  },
  // 局部讀取
  getters: {
    desc: state => "寵物:" + state.name
  },
  // 局部變化
  mutations: {
    increment(state, payload) {
      state.age += payload.num;
    }
  },
  // 局部動做
  actions: {
    grow(context, payload) {
      setTimeout(() => {
        context.commit("increment", payload);
      }, 1000);
    }
  }
};

 

六 HelloWorld.vuecode

 

<template>
  <div class="hello">
    <h3>Vuex狀態樹</h3>
    <div>{{this.$store.state}}</div>
    <h3>mapState</h3>
    <div>{{catName}} {{catAge}}</div>
    <div>{{dogName}} {{dogAge}}</div>
    <h3>mapGetters</h3>
    <div>{{catDesc}}</div>
    <div>{{dogDesc}}</div>
    <h3>mapMutations</h3>
    <button @click="catIncrement({num:1})">貓變化</button>
    <button @click="dogIncrement({num:1})">狗變化</button>
    <h3>mapActions</h3>
    <button @click="catGrow({num:1})">貓動做</button>
    <button @click="dogGrow({num:1})">狗動做</button>
  </div>
</template>

<script>
import { mapState, mapGetters, mapMutations, mapActions } from "vuex";

export default {
  name: "HelloWorld",
  computed: {
    ...mapState("cat", {
      catName: "name",
      catAge: "age"
    }),
    ...mapState("dog", {
      dogName: "name",
      dogAge: "age"
    }),
    ...mapGetters("cat", {
      catDesc: "desc"
    }),
    ...mapGetters("dog", {
      dogDesc: "desc"
    })
  },
  methods: {
    ...mapMutations("cat", { catIncrement: "increment" }),
    ...mapMutations("dog", { dogIncrement: "increment" }),
    ...mapActions("cat", { catGrow: "grow" }),
    ...mapActions("dog", { dogGrow: "grow" })
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
</style>

 

七 運行效果component

 

相關文章
相關標籤/搜索