Vuex module: open namespaces


After the module is switched namespace, the namespace enjoy alone.

 

Copy the code
{ 
    "Module. 1": { 
        State: {}, 
        getters: {}, 
        mutations: {}, 
        Actions: {} 
    }, 
    "Module 2": { 
        State: {}, 
        getters: {}, 
        mutations: {}, 
        Actions : {} 
    } 
}
Copy the code

 

mapState, mapGetters, mapMutations, mapActions first parameter is a string (namespace name), the second parameter is an array (not want to rename) / object (need to rename).

 

Copy the code
mapXXXs ( 'namespace name', [ 'attribute name 1', 'attribute name 2']) 

mapXXXs ( 'namespace name,' { 

  'component of the new name 1': 'Vuex name as the original 1', 

  ' new name assembly 2 ':' Vuex name as the original 2 ' 

})
Copy the code

 

A project structure

 

 

二 main.js

 

Copy the code
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");
Copy the code

 

三 index.js

 

Copy the code
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 }
});
Copy the code

 

四 cat.js

 

Copy the code
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);
    }
  }
};
Copy the code

 

五 dog.js

 

Copy the code
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);
    }
  }
};
Copy the code

 

六 HelloWorld.vue

 

Copy the code
<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>
Copy the code

 

Seven operating results

 

 

Original: https://www.cnblogs.com/sea-breeze/p/11321961.html

Vuex official website: https://vuex.vuejs.org/zh/guide/modules.html

Guess you like

Origin www.cnblogs.com/itgezhu/p/11989781.html