module与moduleCollection你必定要会啊!Vuex源码学习(五)加工后的modulevue
在组件中使用vuex的dispatch和commit的时候,咱们只要把action、mutation注册好,经过dispatch、commit调用一下方法名就能够作到。vuex
vue组件内数组
//in vue component
this.$store.commit('setName',{name : 'xLemon'});
this.$store.commit('list/setName',{name : 'xLemon'});
复制代码
vuex的mutation中bash
// in mutation.js
export const setName = function(state,payload){
state.name = payload.name;
}
复制代码
// in list mutation.js 在名为list的模块下的mutation(有本身的命名空间)
export const setName = function(state,payload){
state.name = payload.name;
}
复制代码
咱们传递的只是一个字符串,commit是如何找到注册mutation时的同名方法的呢?有命名空间的这种mutation是如何被找到而且执行的呢?函数
上源码post
被注册的action和mutation如何被放到对应的属性中的呢?学习
轮到installModule函数要出马了。ui
function installModule (store, rootState, path, module, hot) {
const isRoot = !path.length
// 获取全名
const namespace = store._modules.getNamespace(path)
// register in namespace map
if (module.namespaced) {
// 设置命名空间对照map
store._modulesNamespaceMap[namespace] = module
//console.log(store._modulesNamespaceMap);
}
// set state
if (!isRoot && !hot) {
const parentState = getNestedState(rootState, path.slice(0, -1))
const moduleName = path[path.length - 1]
// 把子模块的state(数据)绑定到父模块上(按照层级)
store._withCommit(() => {
Vue.set(parentState, moduleName, module.state)
})
}
const local = module.context = makeLocalContext(store, namespace, path)
// 使用模块暴露出来的方法来注册mutation、action、getter
module.forEachMutation((mutation, key) => {
const namespacedType = namespace + key
registerMutation(store, namespacedType, mutation, local)
})
module.forEachAction((action, key) => {
const type = action.root ? key : namespace + key
const handler = action.handler || action
registerAction(store, type, handler, local)
})
module.forEachGetter((getter, key) => {
const namespacedType = namespace + key
registerGetter(store, namespacedType, getter, local)
})
module.forEachChild((child, key) => {
installModule(store, rootState, path.concat(key), child, hot)
})
}
复制代码
这个函数虽然只有40多行,但处于一个承上启下的关键点,这一章只会分析如何收集mutation与action其他的内容会再下一章讲述。this
installModule首先获取一下这个模块的命名(我称之为全名) 依赖_modules(ModuleCollection实例对象)的getNamespace方法。spa
效果:
为何会是这样?分析一下代码
getNamespace (path) {
let module = this.root //根模块
return path.reduce((namespace, key) => {
//根模块的path是个空数组不执行
// path的第一项是根模块的儿子模块。
// 获取儿子模块 而且将替换module (path的下一项就是儿子模块中的子模块了)
// 下次累加 就是这个module(轮到子模块了)去找它(子模块)的子模块
module = module.getChild(key)
// 查看儿子模块是否是设置了命名空间
//若是设置了这个模块的全名就增长本身的模块名和一个'/'分割后面的模块名,
//没有的话返回一个'',
// reduce累加能够把这个名称进行累加
return namespace + (module.namespaced ? key + '/' : '')
}, '')
}
复制代码
获取完模块的全名了,以后咱们看一下这两个函数
在上一章节module提供了遍历本身内部的action、mutation的方法。
module.forEachMutation((mutation, key) => {
const namespacedType = namespace + key
registerMutation(store, namespacedType, mutation, local)
})
module.forEachAction((action, key) => {
const type = action.root ? key : namespace + key
const handler = action.handler || action
registerAction(store, type, handler, local)
})
复制代码
const namespacedType = namespace + key
这句话 就是拼接出真正的mutation、action的名字
模块全名+mutation/action的名字。也就是一开始我举例的list/setName是这个mutation的全名(被调用的时候用)
this.$store.commit('list/setName',{name : 'xLemon'});
复制代码
名称已经获取到了,下一步怎么办?
把这些函数按照对应名字放到以前说的_actions、_mutations属性中啊
小彩蛋 设置两个不一样模块的同名mutation(全名同样哦)这两个mutation都会执行,action也是同样的。
action和mutation在被dispatch和commit调用前,
下一章:咱们讨论action和mutation如何被调用的(调用篇)。