Vuex 中的四个map函数
对于这四个函数的理解:能使代码更加简洁,而且不用重复写
mapState
函数最终返回一个对象,适用于Vue 的computed中。
var mapState = normalizeNamespace(function (namespace, states) {
var res = {};
normalizeMap(states).forEach(function (ref) {
var key = ref.key;
var val = ref.val;
res[key] = function mappedState() {
var state = this.$store.state;
var getters = this.$store.getters;
if (namespace) {
var module = getModuleByNamespace(this.$store, 'mapState', namespace);
if (!module) {
return
}
state = module.context.state;
getters = module.context.getters;
}
return typeof val === 'function'
? val.call(this, state, getters)
: state[val]
};
res[key].vuex = true;
});
return res
});
mapGetters
函数最终返回一个对象,适用于Vue 的computed中。
var mapGetters = normalizeNamespace(function (namespace, getters) {
var res = {};
normalizeMap(getters).forEach(function (ref) {
var key = ref.key;
var val = ref.val;
val = namespace + val;
res[key] = function mappedGetter() {
if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) {
return
}
if (!(val in this.$store.getters)) {
console.error(("[vuex] unknown getter: " + val));
return
}
return this.$store.getters[val]
};
res[key].vuex = true;
});
return res
});
mapMutations
函数最终返回一个对象,适用于Vue 的methods中。
var mapMutations = normalizeNamespace(function (namespace, mutations) {
var res = {};
normalizeMap(mutations).forEach(function (ref) {
var key = ref.key;
var val = ref.val;
res[key] = function mappedMutation() {
var args = [], len = arguments.length;
while (len--) args[len] = arguments[len];
var commit = this.$store.commit;
if (namespace) {
var module = getModuleByNamespace(this.$store, 'mapMutations', namespace);
if (!module) {
return
}
commit = module.context.commit;
}
return typeof val === 'function'
? val.apply(this, [commit].concat(args))
: commit.apply(this.$store, [val].concat(args))
};
});
return res
});
mapActions
函数最终返回一个对象,适用于Vue 的methods中。
var mapActions = normalizeNamespace(function (namespace, actions) {
var res = {};
normalizeMap(actions).forEach(function (ref) {
var key = ref.key;
var val = ref.val;
res[key] = function mappedAction() {
var args = [], len = arguments.length;
while (len--) args[len] = arguments[len];
var dispatch = this.$store.dispatch;
if (namespace) {
var module = getModuleByNamespace(this.$store, 'mapActions', namespace);
if (!module) {
return
}
dispatch = module.context.dispatch;
}
return typeof val === 'function'
? val.apply(this, [dispatch].concat(args))
: dispatch.apply(this.$store, [val].concat(args))
};
});
return res
});
举个列子
let module = {
state: {
person: {
name: '天罗',
age: 23,
sex: 'boy',
},
skill: {
Vue: "熟悉Vue流程",
Vuex: "熟练使用",
VueRouter: "熟练使用",
javaScirpt: "熟练底层知识",
HTML: "用用够",
Css: "画画够用"
}
},
getters: {
getPerson: function (state) {
return state.person;
},
getSkillPoint: function (state) {
return function (type) {
return state.skill[type]
}
}
},
actions: {
modifyPerson: function (obj, payload, cb) {
this.commit('modifyPerson', payload);
cb();
}
},
mutations: {
modifyPerson: function (state, payload) {
for (let key in payload) {
if ((key in state.person)) {
state.person[key] = payload.key;
}
}
}
}
}
Vue.use(Vuex);
let mapState = Vuex.mapState;
let mapGetters = Vuex.mapGetters;
let mapActions = Vuex.mapActions;
let mapMutations = Vuex.mapMutations;
let store = new Vuex.store(module);
let e = new Vue({
el: "#app",
computed: {
...mapState({
user: "person",
skill: function (state, getters) {
return state.skill
}
}),
...mapGetters({
Person: "getPerson",
Skill: "getSkillPoint"
})
},
methods: {
...mapMutations({
modifyPersonMutation: function (commit, a, b) {
}
}),
...mapActions({
modifyPersonAction: "modifyPerson"
})
},
})
初始见到map的时候配合着ES6语法,有一种畏惧感,走进它的内部,细致的挖掘,也不过如此,更加透彻的了解了它。在使用过程中更加自信,写出更优质的代码。