user.config.ts文件里提供的默认配置:
userRoutingConfig
userCmsConfig
userTableConfigFactory - function
provideDefaultConfig的实现:
export function provideDefaultConfig(config: any = {}): ValueProvider { return { provide: DefaultConfigChunk, useValue: config, multi: true, }; }
运行时,作为 SAP Spartacus global configuration object这个庞大的配置对象,就是从一个个module里提供的默认配置,一点点合并而成的。
import { CommonModule } from '@angular/common'; import { NgModule } from '@angular/core'; import { ConfigModule } from '../../../config/config.module'; import { COST_CENTERS_NORMALIZER, COST_CENTER_NORMALIZER, COST_CENTER_SERIALIZER, } from '../../../cost-center/connectors/cost-center/converters'; import { OccCostCenterListNormalizer } from './converters/occ-cost-center-list-normalizer'; import { OccCostCenterNormalizer } from './converters/occ-cost-center-normalizer'; import { OccCostCenterSerializer } from './converters/occ-cost-center-serializer'; import { defaultOccCostCentersConfig } from './default-occ-cost-centers-config'; @NgModule({ imports: [CommonModule, ConfigModule.withConfig(defaultOccCostCentersConfig)], providers: [ { provide: COST_CENTERS_NORMALIZER, useExisting: OccCostCenterListNormalizer, multi: true, }, { provide: COST_CENTER_NORMALIZER, useExisting: OccCostCenterNormalizer, multi: true, }, { provide: COST_CENTER_SERIALIZER, useExisting: OccCostCenterSerializer, multi: true, }, ], }) export class CostCenterOccModule {}
withConfig的实现:
import { ModuleWithProviders, NgModule } from '@angular/core'; import { provideConfig, provideConfigFactory } from './config-providers'; import { ConfigurationService } from './services/configuration.service'; @NgModule({}) export class ConfigModule { // To make sure ConfigurationService will be instantiated, we inject it into // module constructor constructor(_configurationService: ConfigurationService) {} /** * Import ConfigModule and contribute config to the global configuration * * To provide default configuration in libraries provideDefaultConfig should be used instead. * * @param config Config object to merge with the global configuration */ static withConfig(config: object): ModuleWithProviders<ConfigModule> { return { ngModule: ConfigModule, providers: [provideConfig(config)], }; } /** * Import ConfigModule and contribute config to the global configuration using factory function * * To provide default configuration in libraries provideDefaultConfigFactory should be used instead. * * @param configFactory Factory function that will generate configuration * @param deps Optional dependencies to factory function */ static withConfigFactory( configFactory: Function, deps?: any[] ): ModuleWithProviders<ConfigModule> { return { ngModule: ConfigModule, providers: [provideConfigFactory(configFactory, deps)], }; } /** * Module with providers, should be imported only once, if possible, at the root of the app. * * @param config */ static forRoot(config: any = {}): ModuleWithProviders<ConfigModule> { return { ngModule: ConfigModule, providers: [provideConfig(config)], }; } }