以AsmStoreModule为例:import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { EffectsModule } from '@ngrx/effects';
import { StoreModule } from '@ngrx/store';
import { StateConfig, StorageSyncType } from '../../state/config/state-config';
import { StateModule } from '../../state/state.module';
import { ASM_FEATURE } from './asm-state';
import { effects } from './effects/index';
import { metaReducers, reducerProvider, reducerToken } from './reducers/index';
import { provideDefaultConfigFactory } from '../../config/config-providers';
export function asmStoreConfigFactory(): StateConfig {
const config: StateConfig = {
state: {
storageSync: {
keys: {
'asm.asmUi': StorageSyncType.LOCAL_STORAGE,
'asm.csagentToken.value.access_token': StorageSyncType.LOCAL_STORAGE,
'asm.csagentToken.value.token_type': StorageSyncType.LOCAL_STORAGE,
'asm.csagentToken.value.expires_in': StorageSyncType.LOCAL_STORAGE,
'asm.csagentToken.value.expiration_time':
StorageSyncType.LOCAL_STORAGE,
'asm.csagentToken.value.scope': StorageSyncType.LOCAL_STORAGE,
'asm.csagentToken.value.userId': StorageSyncType.LOCAL_STORAGE,
},
},
},
};
return config;
}
@NgModule({
imports: [
CommonModule,
HttpClientModule,
StateModule,
StoreModule.forFeature(ASM_FEATURE, reducerToken, { metaReducers }),
EffectsModule.forFeature(effects),
],
providers: [
provideDefaultConfigFactory(asmStoreConfigFactory),
reducerProvider,
],
})
export class AsmStoreModule {}里面有一个asmStoreConfigFactory,这个工厂函数返回了一个config对象:
在@NgModule修饰的AsmStoreModule里面,providers区域的定义,将该工厂函数传入了另一个helper函数:
这个helper函数只是把调用者传入的configFactory和deps,组成成providers数组接收的对象里的useFactory和deps字段。另两个字段provide传入的是硬编码好的DefaultConfigChunk这个injection token和multi为true的标志位。
观察一下运行时,这个asmStoreConfigFactory是如何被调用的:我们在module里实现的factory方法调用完毕之后,返回的config对象: