• Vuex模块:不开启命名空间


    模块不开启命名空间时,会共享全局命名空间。

    {
      state: {
        模块1: "局部状态1",
        模块2: "局部状态2"
      },
      getters: {
        getter1() {},
        getter2() {}
      },
    
      mutations: ["局部变化1", "局部变化2"],
      actions: ["局部动作1", "局部动作2"]
    }

    mapState、mapGetters、mapMutations、mapActions参数是数组。

    mapXXXs(['属性名1','属性名2'])

    一 项目结构

    二 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.js

    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.js

    export default {
      // 局部状态
      state: {
        name: "蓝白英短",
        age: 1
      },
      // 局部读取
      getters: {
        catDesc:  state => "宠物:" + state.name
      },
      // 局部变化
      mutations: {
        increment(state, payload) {
          state.age += payload.num;
        }
      },
      // 局部动作
      actions: {
        grow(context, payload) {
          setTimeout(() => {
            context.commit("increment", payload);
          }, 1000);
        }
      }
    };

    五 dog.js

    export default {
      // 局部状态
      state: {
        name: "拉布拉多",
        age: 1
      },
      // 局部读取
      getters: {
        dogDesc: state => "宠物:" + state.name
      },
      // 局部变化
      mutations: {
        increment(state, payload) {
          state.age += payload.num;
        }
      },
      // 局部动作
      actions: {
        grow(context, payload) {
          setTimeout(() => {
            context.commit("increment", payload);
          }, 1000);
        }
      }
    };

    六 HelloWorld.vue

    <template>
      <div class="hello">
        <h3>Vuex状态树</h3>
        <div>{{this.$store.state}}</div>
        <h3>mapState</h3>
        <div>{{cat}}</div>
        <div>{{dog}}</div>
        <h3>mapGetters</h3>
        <div>{{catDesc}}</div>
        <div>{{dogDesc}}</div>
        <h3>mapMutations</h3>
        <!-- 猫狗的年龄都变化 -->
        <button @click="increment({num:1})">变化</button>
        <h3>mapActions</h3>
        <!-- 猫狗的年龄都变化 -->
        <button @click="grow({num:1})">动作</button>
      </div>
    </template>
    
    <script>
    import { mapState, mapGetters, mapMutations, mapActions } from "vuex";
    
    export default {
      name: "HelloWorld",
      computed: {
        ...mapState(["cat", "dog"]),
        ...mapGetters(["catDesc", "dogDesc"])
      },
      methods: {
        ...mapMutations(["increment"]),
        ...mapActions(["grow"])
      }
    };
    </script>
    
    <!-- Add "scoped" attribute to limit CSS to this component only -->
    <style scoped lang="scss">
    </style>

    七 运行效果

     

  • 相关阅读:
    浅谈线段树
    并查集最简单讲解
    mysql索引原理深度解析
    算法、数据结构可视化
    php算法题---对称的二叉树
    php算法题---连续子数组的最大和
    Jsoup一个简短的引论——采用Java抓取网页数据
    宏定义详细信息
    解决java.sql.SQLException: ORA-01789: query block has incorrect number of result columns
    BZOJ 2435 NOI2011 道路建设 BFS/DFS
  • 原文地址:https://www.cnblogs.com/sea-breeze/p/11320484.html
Copyright © 2020-2023  润新知