Skip to content

Commit c19a06a

Browse files
committed
动态注册,注销 module
1 parent bda0716 commit c19a06a

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

vuex3/module/module-collection.js

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default class ModuleCollection {
3232
update([], this.root, rawRootModule)
3333
}
3434

35+
// 注册 module
3536
register (path /* module 路径*/, rawModule /* module 定义 */, runtime = true /* 动态注册 */) {
3637
if (__DEV__) {
3738
assertRawModule(path, rawModule)
@@ -55,6 +56,7 @@ export default class ModuleCollection {
5556
}
5657
}
5758

59+
// 注销 module
5860
unregister (path) {
5961
const parent = this.get(path.slice(0, -1))
6062
const key = path[path.length - 1]
@@ -77,6 +79,7 @@ export default class ModuleCollection {
7779
parent.removeChild(key)
7880
}
7981

82+
// 判断 module 是否注册
8083
isRegistered (path) {
8184
const parent = this.get(path.slice(0, -1))
8285
const key = path[path.length - 1]

vuex3/store.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class Store {
7272
// initialize the store vm, which is responsible for the reactivity
7373
// (also registers _wrappedGetters as computed properties)
7474

75-
// 设置 store._vm,实例一个 Vue 对象来使 state 以及 getters 变为响应式 */
75+
// 设置 store._vm,实例一个 Vue 对象来使 state 以及 getters 变为响应式
7676
resetStoreVM(this, state)
7777

7878
// apply plugins
@@ -226,32 +226,41 @@ export class Store {
226226
})
227227
}
228228

229-
registerModule (path, rawModule, options = {}) {
230-
if (typeof path === 'string') path = [path]
229+
// API,注册一个 module,当业务进行异步加载的时候,可以通过该接口进行动态注册 module
230+
registerModule (path, rawModule /* module 定义 */, options = {}) {
231+
if (typeof path === 'string') path = [path] // 转化成数组
231232

232233
if (__DEV__) {
233234
assert(Array.isArray(path), `module path must be a string or an Array.`)
235+
// 不允许注册根 module
234236
assert(path.length > 0, 'cannot register the root module by using registerModule.')
235237
}
236238

239+
// 注册 module
237240
this._modules.register(path, rawModule)
241+
// 安装 module
238242
installModule(this, this.state, path, this._modules.get(path), options.preserveState)
239243
// reset store to update getters...
244+
// 重新设置 store._vm
240245
resetStoreVM(this, this.state)
241246
}
242247

248+
// API,注销一个 module
243249
unregisterModule (path) {
244-
if (typeof path === 'string') path = [path]
250+
if (typeof path === 'string') path = [path] // 转化成数组
245251

246252
if (__DEV__) {
247253
assert(Array.isArray(path), `module path must be a string or an Array.`)
248254
}
249255

256+
// 注销 module
250257
this._modules.unregister(path)
258+
// 移除 module 的 state
251259
this._withCommit(() => {
252-
const parentState = getNestedState(this.state, path.slice(0, -1))
253-
Vue.delete(parentState, path[path.length - 1])
260+
const parentState = getNestedState(this.state, path.slice(0, -1)) // 获取父级 state
261+
Vue.delete(parentState, path[path.length - 1]) // 从父级中删除
254262
})
263+
// 重新设置 store._vm
255264
resetStore(this)
256265
}
257266

0 commit comments

Comments
 (0)