exportconst mapState = normalizeNamespace((namespace, states) => { const res = {} if (__DEV__ && !isValidMap(states)) { console.error('[vuex] mapState: mapper parameter must be either an Array or an Object') } normalizeMap(states).forEach(({ key, val }) => { res[key] = functionmappedState() { let state = this.$store.state let getters = this.$store.getters if (namespace) { constmodule = getModuleByNamespace(this.$store, 'mapState', namespace) if (!module) { return } state = module.context.state getters = module.context.getters } returntypeof val === 'function' ? val.call(this, state, getters) : state[val] } // mark vuex getter for devtools res[key].vuex = true }) return res })
最前面定义了一个res对象,用于保存所有的映射结果,最后返回的就是这个对象。
1 2 3
if (__DEV__ && !isValidMap(states)) { console.error('[vuex] mapState: mapper parameter must be either an Array or an Object') }
if来判断第二个参数是不是合法的映射对象,不是就要报错。
1 2 3 4 5 6 7
normalizeMap(states).forEach(({ key, val }) => { res[key] = functionmappedState() { // ... } // mark vuex getter for devtools res[key].vuex = true })
exportconst mapGetters = normalizeNamespace((namespace, getters) => { const res = {} if (__DEV__ && !isValidMap(getters)) { console.error('[vuex] mapGetters: mapper parameter must be either an Array or an Object') } normalizeMap(getters).forEach(({ key, val }) => { // The namespace has been mutated by normalizeNamespace val = namespace + val res[key] = functionmappedGetter() { if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) { return } if (__DEV__ && !(val inthis.$store.getters)) { console.error(`[vuex] unknown getter: ${val}`) return } returnthis.$store.getters[val] } // mark vuex getter for devtools res[key].vuex = true }) return res })
exportconst mapMutations = normalizeNamespace((namespace, mutations) => { const res = {} if (__DEV__ && !isValidMap(mutations)) { console.error('[vuex] mapMutations: mapper parameter must be either an Array or an Object') } normalizeMap(mutations).forEach(({ key, val }) => { res[key] = functionmappedMutation(...args) { // Get the commit method from store let commit = this.$store.commit if (namespace) { constmodule = getModuleByNamespace(this.$store, 'mapMutations', namespace) if (!module) { return } commit = module.context.commit } returntypeof val === 'function' ? val.apply(this, [commit].concat(args)) : commit.apply(this.$store, [val].concat(args)) } }) return res })
exportconst mapActions = normalizeNamespace((namespace, actions) => { const res = {} if (__DEV__ && !isValidMap(actions)) { console.error('[vuex] mapActions: mapper parameter must be either an Array or an Object') } normalizeMap(actions).forEach(({ key, val }) => { res[key] = functionmappedAction(...args) { // get dispatch function from store let dispatch = this.$store.dispatch if (namespace) { constmodule = getModuleByNamespace(this.$store, 'mapActions', namespace) if (!module) { return } dispatch = module.context.dispatch } returntypeof val === 'function' ? val.apply(this, [dispatch].concat(args)) : dispatch.apply(this.$store, [val].concat(args)) } }) return res })
/** * Return a function expect two param contains namespace and map. * it will normalize the namespace and then * the param's function will handle the new namespace and the map. */