久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

javascript - 請(qǐng)問下面的函數(shù)寫法什么意思?

瀏覽:178日期:2023-05-19 10:04:07

問題描述

在vuex中的mutations中定義的一個(gè)函數(shù),在組件中調(diào)用

//store.js在mutations中定義addCart:function (state,{goodIndex,foodIndex}) { state.goods[goodIndex].foods[foodIndex].count++; },

//組件中調(diào)用methods:{ ...mapMutations([’addCart’,’removeCart’,’setCart’]), addCartItem:function(){this.setCart({goodIndex:this.goodIndex,foodIndex:this.foodIndex}); }}

我的問題是為什么在調(diào)用setCart函數(shù)的時(shí)候不用傳入state參數(shù),目測(cè)如果調(diào)用的時(shí)候不傳state參數(shù)的話,addCart函數(shù)執(zhí)行的時(shí)候就會(huì)自動(dòng)將在store中的state傳入進(jìn)去,這樣的原理是什么??這是自己半個(gè)月前寫的代碼,現(xiàn)在看怎么也不理解了。。

問題解答

回答1:

去看看源碼就知道了。

export const mapMutations = normalizeNamespace((namespace, mutations) => { const res = {} normalizeMap(mutations).forEach(({ key, val }) => { val = namespace + val res[key] = function mappedMutation (...args) { if (namespace && !getModuleByNamespace(this.$store, ’mapMutations’, namespace)) {return } // 在這里調(diào)用了commit方法 return this.$store.commit.apply(this.$store, [val].concat(args)) } }) return res})

下面是commit方法的定義

this.commit = function boundCommit (type, payload, options) { // store 就是你想要的答案 return commit.call(store, type, payload, options)}回答2:

this.setCart()被映射為this.$store.commit(’setCart’)

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 新津县| 平陆县| 屏山县| 黔西县| 句容市| 青州市| 大田县| 涿鹿县| 侯马市| 怀宁县| 盘山县| 巨鹿县| 北流市| 读书| 江永县| 额济纳旗| 兴业县| 彰化市| 郸城县| 关岭| 桐乡市| 日土县| 翼城县| 蚌埠市| 永善县| 若羌县| 神池县| 买车| 子洲县| 白沙| 邢台市| 广宗县| 昌宁县| 新沂市| 响水县| 仁怀市| 垦利县| 黄山市| 筠连县| 长阳| 尤溪县|