state的树状结构是在何时构建的
来源:12-12 API(2)

center_one
2020-04-24
state的获取
get state () {
return this._vm._data.$$state
}
state的响应式
store._vm = new Vue({
data: {
$$state: state
},
computed
})
storevm的注册
var state = this._modules.root.state;
resetStoreVM(this, state);
模块初始化
this._modules = new ModuleCollection(options);
注册方法中执行了
const newModule = new Module(rawModule, runtime)
Module的构造函数中
const rawState = rawModule.state
this.state = (typeof rawState === ‘function’ ? rawState() : rawState) || {}
问题:根模块的state应该是配置时最外层的state,为何最终的store中的state是树状结构,包含了子模块的state
2回答
-
因为后面会执行 installModule 方法,它通过递归的方式构造出这样的树状结构。
012020-04-25 -
center_one
提问者
2020-04-25
if (!isRoot && !hot) {
const parentState = getNestedState(rootState, path.slice(0, -1))
const moduleName = path[path.length - 1]
store._withCommit(() => {
Vue.set(parentState, moduleName, module.state)
})
}00
相似问题