1.什么是mutations?
上一篇文章說(shuō)的getters
是為了初步獲取和簡(jiǎn)單處理state
里面的數(shù)據(jù)(這里的簡(jiǎn)單處理不能改變state里面的數(shù)據(jù)),Vue
的視圖是由數(shù)據(jù)驅(qū)動(dòng)的,也就是說(shuō)state
里面的數(shù)據(jù)是動(dòng)態(tài)變化的,那么怎么改變呢,切記在Vuex
中store
數(shù)據(jù)改變的唯一方法就是mutation
!
通俗的理解mutations
,里面裝著一些改變數(shù)據(jù)方法的集合,這是Veux
設(shè)計(jì)很重要的一點(diǎn),就是把處理數(shù)據(jù)邏輯方法全部放在mutations
里面,使得數(shù)據(jù)和視圖分離。
2.怎么用mutations?
mutation結(jié)構(gòu):每一個(gè)mutation
都有一個(gè)字符串類(lèi)型的事件類(lèi)型(type
)和回調(diào)函數(shù)(handler
),也可以理解為{type:handler()},
這和訂閱發(fā)布有點(diǎn)類(lèi)似。先注冊(cè)事件,當(dāng)觸發(fā)響應(yīng)類(lèi)型的時(shí)候調(diào)用handker()
,調(diào)用type
的時(shí)候需要用到store.commit
方法。
const store = new Vuex.Store({ state: { count: 1 }, mutations: { increment (state) { //注冊(cè)時(shí)間,type:increment,handler第一個(gè)參數(shù)是state; // 變更狀態(tài) state.count++}}}) store.commit('increment') //調(diào)用type,觸發(fā)handler(state)
載荷(payload):簡(jiǎn)單的理解就是往handler(stage)
中傳參handler(stage,pryload)
;一般是個(gè)對(duì)象。
mutations: { increment (state, n) { state.count += n}} store.commit('increment', 10)mutation-types:將常量放在單獨(dú)的文件中,方便協(xié)作開(kāi)發(fā)。 // mutation-types.js export const SOME_MUTATION = 'SOME_MUTATION' // store.jsimport Vuex from 'vuex'import { SOME_MUTATION } from './mutation-types' const store = new Vuex.Store({ state: { ... }, mutations: { // 我們可以使用 ES2015 風(fēng)格的計(jì)算屬性命名功能來(lái)使用一個(gè)常量作為函數(shù)名 [SOME_MUTATION] (state) { // mutate state }}})
commit:提交可以在組件中使用 this.$store.commit('xxx')
提交 mutation
,或者使用 mapMutations
輔助函數(shù)將組件中的 methods
映射為 store.commit
調(diào)用(需要在根節(jié)點(diǎn)注入 store
)。
import { mapMutations } from 'vuex'export default {methods: { ...mapMutations([ 'increment' // 映射 this.increment() 為 this.$store.commit('increment')]), ...mapMutations({ add: 'increment' // 映射 this.add() 為 this.$store.commit('increment') })}}
3.源碼分析
registerMutation
:初始化mutation
function registerMutation (store, type, handler, path = []) { //4個(gè)參數(shù),store是Store實(shí)例,type為mutation的type,handler,path為當(dāng)前模塊路徑 const entry = store._mutations[type] || (store._mutations[type] = []) //通過(guò)type拿到對(duì)應(yīng)的mutation對(duì)象數(shù)組 entry.push(function wrappedMutationHandler (payload) { //將mutation包裝成函數(shù)push到數(shù)組中,同時(shí)添加載荷payload參數(shù) handler(getNestedState(store.state, path), payload) //通過(guò)getNestedState()得到當(dāng)前的state,同時(shí)添加載荷payload參數(shù) }) }
commit
:調(diào)用mutation
commit (type, payload, options) { // 3個(gè)參數(shù),type是mutation類(lèi)型,payload載荷,options配置 if (isObject(type) && type.type) { // 當(dāng)type為object類(lèi)型, options = payload payload = type type = type.type } const mutation = { type, payload } const entry = this._mutations[type] // 通過(guò)type查找對(duì)應(yīng)的mutation if (!entry) { //找不到報(bào)錯(cuò) console.error(`[vuex] unknown mutation type: ${type}`) return } this._withCommit(() => { entry.forEach(function commitIterator (handler) { // 遍歷type對(duì)應(yīng)的mutation對(duì)象數(shù)組,執(zhí)行handle(payload)方法 //也就是開(kāi)始執(zhí)行wrappedMutationHandler(handler) handler(payload) }) }) if (!options || !options.silent) { this._subscribers.forEach(sub => sub(mutation, this.state)) //把mutation和根state作為參數(shù)傳入 }}
subscribers
:訂閱store
的mutation
subscribe (fn) {const subs = this._subscribersif (subs.indexOf(fn) < 0) { subs.push(fn) }return () => { const i = subs.indexOf(fn) if (i > -1) { subs.splice(i, 1) } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注