国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

Vuex之理解Mutations的用法實(shí)例

2019-11-19 16:47:28
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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)變化的,那么怎么改變呢,切記在Vuexstore數(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:訂閱storemutation

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)。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 莱州市| 土默特右旗| 大理市| 治多县| 东山县| 马龙县| 永嘉县| 霍林郭勒市| 海丰县| 岐山县| 镇雄县| 荆门市| 金沙县| 河曲县| 大渡口区| 海伦市| 永济市| 台州市| 宕昌县| 禹州市| 监利县| 怀仁县| 莱州市| 香港 | 钟山县| 攀枝花市| 稻城县| 大兴区| 手游| 鄱阳县| 西藏| 习水县| 鲜城| 唐山市| 隆化县| 满洲里市| 勃利县| 贵德县| 新巴尔虎左旗| 平顺县| 浮梁县|