@@ -72,7 +72,7 @@ export class Store {
72
72
// initialize the store vm, which is responsible for the reactivity
73
73
// (also registers _wrappedGetters as computed properties)
74
74
75
- // 设置 store._vm,实例一个 Vue 对象来使 state 以及 getters 变为响应式 */
75
+ // 设置 store._vm,实例一个 Vue 对象来使 state 以及 getters 变为响应式
76
76
resetStoreVM ( this , state )
77
77
78
78
// apply plugins
@@ -226,32 +226,41 @@ export class Store {
226
226
} )
227
227
}
228
228
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 ] // 转化成数组
231
232
232
233
if ( __DEV__ ) {
233
234
assert ( Array . isArray ( path ) , `module path must be a string or an Array.` )
235
+ // 不允许注册根 module
234
236
assert ( path . length > 0 , 'cannot register the root module by using registerModule.' )
235
237
}
236
238
239
+ // 注册 module
237
240
this . _modules . register ( path , rawModule )
241
+ // 安装 module
238
242
installModule ( this , this . state , path , this . _modules . get ( path ) , options . preserveState )
239
243
// reset store to update getters...
244
+ // 重新设置 store._vm
240
245
resetStoreVM ( this , this . state )
241
246
}
242
247
248
+ // API,注销一个 module
243
249
unregisterModule ( path ) {
244
- if ( typeof path === 'string' ) path = [ path ]
250
+ if ( typeof path === 'string' ) path = [ path ] // 转化成数组
245
251
246
252
if ( __DEV__ ) {
247
253
assert ( Array . isArray ( path ) , `module path must be a string or an Array.` )
248
254
}
249
255
256
+ // 注销 module
250
257
this . _modules . unregister ( path )
258
+ // 移除 module 的 state
251
259
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 ] ) // 从父级中删除
254
262
} )
263
+ // 重新设置 store._vm
255
264
resetStore ( this )
256
265
}
257
266
0 commit comments