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

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

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

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

問題描述

在vuex中的mutations中定義的一個函數,在組件中調用

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

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

我的問題是為什么在調用setCart函數的時候不用傳入state參數,目測如果調用的時候不傳state參數的話,addCart函數執行的時候就會自動將在store中的state傳入進去,這樣的原理是什么??這是自己半個月前寫的代碼,現在看怎么也不理解了。。

問題解答

回答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 } // 在這里調用了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
相關文章:
主站蜘蛛池模板: 新干县| 涡阳县| 井冈山市| 宁南县| 庆安县| 五莲县| 炎陵县| 临颍县| 武陟县| 苏尼特左旗| 麦盖提县| 平安县| 左贡县| 湖口县| 中阳县| 刚察县| 潜山县| 桃园市| 武夷山市| 大关县| 林甸县| 长顺县| 舒兰市| 内乡县| 保山市| 区。| 大同县| 宁津县| 扶绥县| 东乌珠穆沁旗| 盐源县| 民权县| 阳西县| 米林县| 富顺县| 张家界市| 宁海县| 讷河市| 镇远县| 怀集县| 阿鲁科尔沁旗|