zoukankan      html  css  js  c++  java
  • VUE实例课程---42、counter实例_vuex_简写

    VUE实例课程---42、counter实例_vuex_简写

    一、总结

    一句话总结:

    可以用mapState、mapActions、mapGetters等来代替this.$store.state、this.$store.dispatch、this.$store.getters等,这样可以极大的简化代码
    <template>
        <div>
            <div>click {{count}} times, count is {{evenOrOdd}}</div>
            <button @click="increment">+</button>
            <button @click="decrement">-</button>
            <button @click="incrementIfOdd">increment if odd</button>
            <button @click="incrementAsync">increment async</button>
            <button @click="increment3">increment 3</button>
        </div>
    </template>
    
    <script>
        import {mapState,mapActions,mapGetters} from 'vuex';
        export default {
            name: "Counter",
            data:function () {
                return {};
            },
            computed:{
                ...mapState(['count']) 的运行过程是什么
                先调用, //mapState()返回值:{count(){ return this.$store.state['count']}}
                ...mapGetters(['evenOrOdd']), //mapGetters()返回值:{evenOrOdd(){ this.$store.getters['evenOrOdd'] }}
            },
            methods:{
                ...mapActions(['increment','decrement','incrementIfOdd','incrementAsync','increment3'])
            }
        }
    </script>
    
    <style scoped>
    button{
        margin-right: 10px;
    }
    </style>

    1、...mapState(['count']) 的运行过程是什么?

    先调用mapState()方法,得到一个对象,对象就类似{count(){ return this.$store.state['count']}},然后通过前面的三点运算符,可以成功得到vuex中的state中的count属性,从而获取数据

    2、组件中调用vuex的actions的简写方式?

    可以写成 ...mapActions(['increment','decrement','incrementIfOdd','incrementAsync','increment3']) 的形式
      methods:{
          ...mapActions(['increment','decrement','incrementIfOdd','incrementAsync','increment3'])
      }
      // methods:{
      //     increment:function () {
      //         this.$store.dispatch('increment')
      //     },
      //     decrement:function () {
      //         this.$store.dispatch('decrement')
      //     },
      //     incrementIfOdd:function () {
      //         this.$store.dispatch('incrementIfOdd')
      //     },
      //     incrementAsync:function () {
      //         this.$store.dispatch('incrementAsync')
      //     },
      //     increment3:function () {
      //         this.$store.dispatch('increment3')
      //     }
      // }

    二、counter实例_vuex_简写

    博客对应课程的视频位置:

    1、Counter.vue

     1 <template>
     2     <div>
     3         <div>click {{count}} times, count is {{evenOrOdd}}</div>
     4         <button @click="increment">+</button>
     5         <button @click="decrement">-</button>
     6         <button @click="incrementIfOdd">increment if odd</button>
     7         <button @click="incrementAsync">increment async</button>
     8         <button @click="increment3">increment 3</button>
     9 <!--        <button @click="test1">test1</button>-->
    10     </div>
    11 </template>
    12 
    13 <script>
    14     import {mapState,mapActions,mapGetters} from 'vuex';
    15     export default {
    16         name: "Counter",
    17         data:function () {
    18             return {};
    19             // return {
    20             //     count:1
    21             // };
    22         },
    23         computed:{
    24             ...mapState(['count']), //mapState()返回值:{count(){ return this.$store.state['count']}}
    25             ...mapGetters(['evenOrOdd']), //mapGetters()返回值:{evenOrOdd(){ this.$store.getters['evenOrOdd'] }}
    26             // evenOrOdd(){
    27             //     return this.$store.getters.evenOrOdd;
    28             // },
    29             // count(){
    30             //     return this.$store.state.count;
    31             // }
    32         },
    33         methods:{
    34             ...mapActions(['increment','decrement','incrementIfOdd','incrementAsync','increment3']),
    35             // test1(){
    36             //     let map_action=mapActions(['increment','decrement','incrementIfOdd','incrementAsync','increment3']);
    37             //     console.log(map_action);
    38             //     for (let i in map_action){
    39             //         console.log(i);
    40             //         console.log(map_action[i]);
    41             //     }
    42             // }
    43         }
    44         // methods:{
    45         //     increment:function () {
    46         //         console.log(mapState);
    47         //         console.log(mapState(['count']));
    48         //         for (let i in mapState(['count'])){
    49         //             console.log(i);
    50         //             console.log(mapState(['count'])[i])
    51         //         }
    52         //         this.$store.dispatch('increment')
    53         //     },
    54         //     decrement:function () {
    55         //         // this.count--;
    56         //         this.$store.dispatch('decrement')
    57         //     },
    58         //     incrementIfOdd:function () {
    59         //         // if(this.count%2===1){
    60         //         //     this.count++;
    61         //         // }
    62         //         this.$store.dispatch('incrementIfOdd')
    63         //     },
    64         //     incrementAsync:function () {
    65         //         // setTimeout(()=>{
    66         //         //     this.count++;
    67         //         // },1000);
    68         //         this.$store.dispatch('incrementAsync')
    69         //     },
    70         //     increment3:function () {
    71         //         this.$store.dispatch('increment3')
    72         //     }
    73         // }
    74     }
    75 </script>
    76 
    77 <style scoped>
    78 button{
    79     margin-right: 10px;
    80 }
    81 </style>

    2、store/index.js

     1 import Vue from 'vue'
     2 import Vuex from 'vuex'
     3 
     4 Vue.use(Vuex)
     5 
     6 export default new Vuex.Store({
     7   state: {
     8     //初始化数据
     9     count:0
    10   },
    11   mutations: {
    12     INCREMENT (state,number=1) {
    13       state.count+=number;
    14     },
    15     DECREMENT (state) {
    16       state.count--
    17     }
    18   },
    19   actions: {
    20     increment ({commit}) {
    21       // 提交一个mutation请求
    22       commit('INCREMENT')
    23     },
    24     decrement ({commit}) {
    25       // 提交一个mutation请求
    26       commit('DECREMENT')
    27     },
    28     incrementIfOdd ({commit, state}) {
    29       if(state.count%2===1) {
    30         // 提交一个mutation请求
    31         commit('INCREMENT')
    32       }
    33     },
    34     incrementAsync ({commit}) {
    35       setTimeout(() => {
    36         // 提交一个mutation请求
    37         commit('INCREMENT')
    38       }, 1000)
    39     },
    40     increment3 ({commit}) {
    41       commit('INCREMENT',3);
    42     }
    43   },
    44   getters:{
    45     evenOrOdd (state) { // 当读取属性值时自动调用并返回属性值
    46       return state.count%2===0 ? '偶数' : '奇数'
    47     }
    48   },
    49   modules: {
    50   }
    51 })

     
  • 相关阅读:
    质量属性--信息技术手册
    蓝桥杯赛前整理
    感悟:荔枝架构实践与演进历程
    以《淘宝网》为例,描绘质量属性的六个常见属性场景
    感悟:淘宝架构演进背后——零售业务中台架构设计探讨及实践
    为什么要考研???
    寒假学习笔记03
    寒假学习笔记02
    寒假学习笔记01
    数据清洗与数据处理
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/12802772.html
Copyright © 2011-2022 走看看