• vuex分模块管理


    1.定义命名空间

    dog.js

    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);
        }
      }
    };

    组件中代码
    <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>

    2.未定义命名空间

    dog.js

    export default {
      // 局部状态
      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);
        }
      }
    };

    组件中代码
    <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: state => state.dog.name }), ...mapGetters({ catDesc: "desc" }), ...mapGetters({ dogDesc: "desc" }) }, methods: { ...mapMutations({ catIncrement: "increment" }), ...mapMutations({ dogIncrement: "increment" }), ...mapActions({ catGrow: "grow" }), ...mapActions({ dogGrow: "grow" }) } }; </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped lang="scss"> </style>
    无命名空间时,模块内部的 action、mutation、和 getter 现在仍然注册在全局命名空间——这样保证了多个模块能够响应同一 mutation 或 action。
  • 相关阅读:
    Logstash:input plugin 介绍
    Beats:使用Elastic Stack对Redis监控
    Elastic:Elasticsearch的分片管理策略
    Beats:使用Elastic Stack对Nginx Web服务器监控
    Beats:使用Elastic Stack监控RabbitMQ
    Elasticsearch启动https访问
    Elasticsearch:跨集群复制 Cross-cluster replication(CCR)
    Logstash: 如何创建可维护和可重用的Logstash管道
    Solutions:Elastic workplace 搜索:随时随地搜索所有内容 (二)
    Solutions:Elastic workplace 搜索:随时随地搜索所有内容 (一)
  • 原文地址:https://www.cnblogs.com/lansetiankongblog/p/12023362.html
Copyright © 2020-2023  润新知