第四十一節(jié):Vuex狀態(tài)管理: Mutation的使用

1 Mutations使用

1.1關(guān)于Mutations使用說明:
  1. 更改 Vuex 的 store 中的狀態(tài)的唯一方法是提交 mutation
  2. mutations非常類似于事件,都有一個(gè)字符串的事件類型type和一個(gè)回調(diào)函數(shù)handler
  3. 這個(gè)回調(diào)函數(shù)就是修改狀態(tài)的地方,
  4. store對(duì)象提供了一個(gè)commit方法用來觸發(fā)mutations中的事件, 有點(diǎn)類似于$emit


1.2 定義與使用mutation
1.2.1 定義mutations
let store = new Vuex.Store({
    state:{
        count: 0
    },

    // 定義mutations
    mutations:{
        increment(){
            // console.log(this); //this 是store對(duì)象
            this.state.count++   // 修改狀態(tài)中的數(shù)據(jù)
        }
    }
})


1.2.2 使用mutations函數(shù)修改數(shù)據(jù)
// 組件中
export default {
    // ...
    methods:{
        increment(){
            // 在組件中通過commit觸發(fā)mutations中的函數(shù)
            this.$store.commit("increment")

        },
    }
}


2 關(guān)于mutations函數(shù)的參數(shù)

2.1 mutations參數(shù)說明:
  1. mutations函數(shù)只接受兩個(gè)參數(shù)
  2. mutations函數(shù)可以接受兩個(gè)參數(shù),第一個(gè)參數(shù)就是state狀態(tài),
  3. 第二個(gè)參數(shù)是在通過commit觸發(fā)mutations函數(shù)時(shí)傳遞的載荷(Payload,其實(shí)就是自定義傳參)


2.2 mutations函數(shù)的第一個(gè)參數(shù)

說明:

  1. 上一個(gè)示例中我們是采用了this.state.count++的方式修改數(shù)據(jù)的
  2. mutations函數(shù)接受的第一個(gè)參數(shù)就是state狀態(tài)對(duì)象,因?yàn)槲覀兛梢灾苯油ㄟ^state修改狀態(tài)

示例代碼:

let store = new Vuex.Store({
    state:{
        count: 0
    },

    // 定義mutations
    mutations:{
        increment(state){
            // mutations函數(shù)的第一個(gè)參數(shù)就是state對(duì)象
            // console.log(state);
            
            state.count++
        }
    }
})


2.3 mutations的第二個(gè)參數(shù)

官網(wǎng)關(guān)于第二個(gè)參數(shù)的說法叫做提交載荷(Payload)

也就是說我們?cè)诮M件中使用commit觸發(fā)mutations函數(shù)是,還可以傳入額外的參數(shù)

mutations

let store = new Vuex.Store({
  state:{
    count: 0
  },

  // 定義mutations
  mutations:{
    increment(state, num){
        // 通過第二個(gè)參數(shù)指定state狀態(tài)修改
      state.count += num
    }
  }
})

組件觸發(fā)

export default {

    // ...
    methods:{
        increment(){
            // 觸發(fā)mutations函數(shù)是,指定額外的參數(shù)
            this.$store.commit("increment",2)

        },

    }

}


大多數(shù)情況下,載荷也就是第二個(gè)參數(shù)應(yīng)該是一個(gè)對(duì)象,

原因在于對(duì)象可以傳遞多個(gè)數(shù)據(jù),如果傳遞普通類型的值只能傳遞一個(gè)

export default {

    // ...
    methods:{
        increment(){
            // 觸發(fā)mutations函數(shù)是,指定額外的參數(shù)
            this.$store.commit("increment",{
                num: 2
            })

        },

    }

}


3. 關(guān)于Mutations的風(fēng)格

提交的另外一種風(fēng)格:直接使用包含 type 屬性的對(duì)象:

示例代碼如下

export default {

    // ...
    methods:{
        increment(){
            // 觸發(fā)mutations函數(shù)是,指定額外的參數(shù)
            this.$store.commit({
                type:"increment",
                num: 2
            })

        },

    }

}

此時(shí)Mutations函數(shù)不用做任何改變

let store = new Vuex.Store({
    state:{
        count: 0
    },

    // 定義mutations
    mutations:{
        increment(state, payload){
            console.log(payload);
            /*
            {
                type:"increment",
                num: 2
            }
            */
            state.count += payload.num
        }
    }
})

會(huì)自動(dòng)觸發(fā)payload中type屬性對(duì)應(yīng)的mutations函數(shù), commit參數(shù)對(duì)象整體就是Payload


4. Mutation需要遵循Vue的響應(yīng)規(guī)則

既然 Vuex 的 store 中的狀態(tài)是響應(yīng)式的,那么當(dāng)我們變更狀態(tài)時(shí),監(jiān)視狀態(tài)的 Vue 組件也會(huì)自動(dòng)更新.

因此Vuex中的mutation也需要Vue的響應(yīng)規(guī)則

4.1 觸發(fā)響應(yīng)的注意事項(xiàng)
  1. 最好提前在你的 store 中初始化好所有所需屬性。
  2. 使用 Vue.set(obj, 'newProp', 123)方法新增對(duì)象的屬性
  3. 使用新對(duì)象替換老對(duì)象的方式觸發(fā)響應(yīng)


4.2 示例代碼

利用擴(kuò)展運(yùn)算來創(chuàng)建新對(duì)象替換老對(duì)象

state.obj = { ...state.obj, newProp: 123 }


5. Mutation必須是一個(gè)同步函數(shù)

5.1 同步函數(shù)說明
  1. 一條重要的原則就是要記住 mutation 必須是同步函數(shù)
  2. 如果mutation是一個(gè)異步函數(shù), 異步修改狀態(tài),那么devtools將不能跟蹤到數(shù)據(jù)的變化
  3. 因?yàn)?code>devtools 都需要捕捉到前一狀態(tài)和后一狀態(tài)的快照
  4. 如果是異步函數(shù)將沒發(fā)捕捉到快照信息


5.2 示例代碼
let store = new Vuex.Store({
  state:{
    count: 0
  },

  // 定義mutations
  mutations:{
    increment(state, payload){
      
        //mutation中異步修改狀態(tài)
      setTimeout(() => {
        state.count += payload.num
      },2000)
     
      console.log(this);
 
})

此時(shí)devtools中沒發(fā)監(jiān)聽狀態(tài)state的變化

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容