国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

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

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

問題描述

在vuex中的mutations中定義的一個函數(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ù)的時候不用傳入state參數(shù),目測如果調(diào)用的時候不傳state參數(shù)的話,addCart函數(shù)執(zhí)行的時候就會自動將在store中的state傳入進去,這樣的原理是什么??這是自己半個月前寫的代碼,現(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’)

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 綦江县| 阳曲县| 唐河县| 林口县| 宿州市| 仙游县| 临漳县| 宜阳县| 钦州市| 从江县| 萨迦县| 包头市| 潜山县| 垫江县| 莱州市| 札达县| 永州市| 龙游县| 柏乡县| 炉霍县| 浦东新区| 颍上县| 灵台县| 湖口县| 张家界市| 永丰县| 饶平县| 麟游县| 客服| 德化县| 奉新县| 湖北省| 石台县| 嘉鱼县| 泉州市| 凉山| 孝感市| 鄂托克旗| 潜山县| 文山县| 翁牛特旗|