笔记

课程:
https://www.bilibili.com/video/BV1Zy4y1K7SH?p=113

经验:
看到 commit(大写方法名,参数) ,就应该明白正在和 mutations 对话。
怎么和mutations对话呢?你要调 commit(大写方法名,参数)

为什么呢?因为一切都是从下图出发的。

113_mapActions与mapMutations - 图1

commit(大写方法名,参数) 指:
this.$store.commit(大写方法名,参数)

113_mapActions与mapMutations - 图2


如上图所示,有没有什么东西可以帮我们自动生成 increment(){this.$store.commit(‘大写方法名’,value)} 这一长串呢?
因为调用 this.$store.commit 是为了跟 Mutations 对话,所以我们这里不叫 mapCommit , 而是叫 mapMutations 。

如何使用 mapMutations 呢?
以下图为例:

113_mapActions与mapMutations - 图3


使用方法:

...mapMutations({方法名0:'commit方法名0'},{方法名1:'commit方法名1'})

改写上图 increment(){} 内容为:

...mapMutations({increment:'JIA',decrement:'JIAN'}),

可以在模板中调用 方法名0(参数) 方法名1(参数) 的时候,把参数传递进去。

重要文件源码

components\Count.vue

<template>
    <div>
        <!-- 把上节的插值优化,使用简单的插值语法。 借助:计算属性 -->
        <h1>当前求和为:{{sum}}</h1>
        <h3>当前求和放大10倍为:{{bigSum}}</h3>
        <h3>我在{{school}},学习{{subject}}</h3>

        <select v-model.number="n">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>

        <!-- 
            <button @click="increment">+</button>
            <button @click="decrement">-</button>
            <button @click="incrementOdd">当前求和为奇数再加</button>
            <button @click="incrementWait">等一等再加</button>
        -->


        <!-- 对象写法: 在使用 ...mapMutations()的时候, 需要在调用方法名的时候,把参数传递进去. -->
        <button @click="increment(n)">+</button>
        <button @click="decrement(n)">-</button>

        <!-- 数组写法: 
        <button @click="JIA(n)">+</button>
        <button @click="JIAN(n)">-</button>
        -->


        <!-- 对象写法 -->
        <button @click="incrementOdd(n)">当前求和为奇数再加</button>
        <button @click="incrementWait(n)">等一等再加</button>
        <!-- 数组写法 
        <button @click="jiaOdd(n)">当前求和为奇数再加</button>
        <button @click="jiaWait(n)">等一等再加</button>
        -->
    </div>
</template>

<script>
    //引入 映射状态
    import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'

    export default {
        name:'Count',
        data() {
            return {
                n:1, //用户选择的数字
            }
        },
        computed:{
            //靠 程序员自己 亲自 去写 计算属性
            /* sum(){
                return this.$store.state.sum
            },
            school(){
                return this.$store.state.school
            },
            subject(){
                return this.$store.state.subject
            }, */
            //规律: 上面3个都是从  this.$store.state.*** 取数据。
            //借助 mapState 生成计算属性,从 state 中读取数据。(对象写法)
            // ...mapState({he:'sum',xuexiao:'school',xueke:'subject'}),
            //利用 mapState 自动生成  this.$store.state 代码的函数。
            //借助 mapState 生成计算属性,从 state 中读取数据。(数组写法) ★★★ mapState就是从state中读取数据。
            //ES6 语法:
            //    ... 是一个对象
            //    此处的意思,就会自动生成上面3个函数的内容,并放在这里。 函数名=后缀名(即:计算属性叫 啥,那么就从 this.$store.state 中读取 啥 )
            //...mapState([sum:'sum',school:'school',subject:'subject']),
            //注意: 此处并不是触发了对象的简写形式,因为 value 是字符串 ?
            //数组写法: 1个sum 2种用途。 作为计算属性名 作为 .state中读取的名 
            //  mapState(数组) 自动生成的 计算属性名 等于 从.state中读取的名。
            ...mapState(['sum','school','subject']),


            /* ******************************************************************** */

            /* bigSum(){
                return this.$store.getters.bigSum
            }, */

            //借助 mapGetters 生成计算属性,从 getters 中读取数据。(对象写法)
            // ...mapGetters({bigSum:'bigSum'})

            //借助 mapGetters 生成计算属性,从 getters 中读取数据。(数组写法)
            ...mapGetters(['bigSum'])

        },
        methods: {

            /*increment(){ //方法名0
                this.$store.commit('JIA',    //commit方法名0
                                    this.n)
            },
            decrement(){    //方法名1
                this.$store.commit('JIAN',    //commit方法名1
                                    this.n)
            },*/
            //对象写法
            //使用 mapMutations() 自动生成上面的 increment() 和 decrement()方法,方法中会调用 commit 去联系 mutations ∈ 对象写法 。
            ...mapMutations({increment:'JIA',decrement:'JIAN'}),
            //数组写法:
            //...mapMutations(['JIA',        //可以理解为 方法名0 和 commit方法名0 都是 JIA , 所有上面在模板中调用的时候,也要使用 JIA
            //                'JIAN']),    //可以理解为 方法名1 和 commit方法名1 都是 JIAN ,所有上面在模板中调用的时候,也要使用 JIAN

            // --------------------- WWW.SOFTOOL.CN ---------------------

            //
            /*incrementOdd(){
                this.$store.dispatch('jiaOdd',this.n)
            },
            incrementWait(){
                this.$store.dispatch('jiaWait',this.n)
            },*/
            ...mapActions({incrementOdd:'jiaOdd', incrementWait:'jiaWait'}), //对象写法
            //...mapActions(['jiaOdd', 'jiaWait']) //数组写法
        },

        mounted() {
            //key 是一个字符串, 一般把 'key' 两边的引号省略了。
            const x = mapState({he:'sum',xuexiao:'school',xueke:'subject'})
            console.log(x)
        },
    }
</script>

<style lang="css">
    button{
        margin-left: 5px;
    }
</style>