在 APP_INITIALIZATION 之后使用已解析的配置提供 InjectionToken
Provide an InjectionToken after APP_INITIALIZATION with the resolved configuration
我需要使用将在应用程序初始化期间解析的工厂获取配置(使用 APP_INITIALIZER 提供程序)。
export function loadConfig(): () => Promise<Config> {
// return promised config
}
使用 AppModule 提供:
providers: [{
provide: APP_INITIALIZER,
useFactory: loadConfig,
deps: [HttpClient, ConfigService],
multi: true
}]
然后我需要使用该配置数据在另一个 InjectionToken 中注入一些东西,但是如果我使用应用程序初始化期间提供的配置提供特定的 injectiontoken,则该过程在 APP_INITIALIZER 执行之前执行。
export const FORMATS = new InjectionToken<Formats>("formats")
export assignFormat(configService: ConfigService) {
return configService.getFormats(); // Needs to execute after APP_INITIALIZER, not before
}
providers: [{
provide: APP_INITIALIZER,
useFactory: loadConfig,
deps: [HttpClient, ConfigService],
multi: true
}, {
provide: FORMATS,
useFactory: assignFormat,
deps: [ConfigService]
}]
@Injectable({ providedIn: "root" })
export class ConfigService {
constructor() {}
getFormats() {}
}
APP初始化后如何提供injection token?
如果您的 loadConfig
工厂 return 是一个函数而不是实际的 promise:
,那么您在这里所拥有的实际上应该可以工作,
const loadConfig = (configService: ConfigService) => {
return () =>
new Promise<void>((resolve, reject) => {
// I added the timeout to simulate a delay
setTimeout(() => {
// you might be using a http call to get the config from the server instead.
// Make sure you keep the config that you fetched in the service;
// this way you can inject the service in the next factory function
// and have the config available
configService.config = {
formats: 'formats',
};
resolve();
}, 1000);
});
};
APP_INITIALIZER
的规定与您的代码完全一样:
{
provide: APP_INITIALIZER,
useFactory: loadConfig,
deps: [ConfigService],
multi: true,
},
当您设置下一个注入令牌时,您应该可以使用该配置
{
provide: FORMATS,
useFactory: (configService: ConfigService) => {
// getFormats method must not be async, it needs to return the actual
// formats that were fetched during the app initialization phase
return configService.getFormats();
},
deps: [ConfigService],
},
Angular 中唯一允许的异步工厂是您与 APP_INITIALIZER
injection token 一起使用的那些,但请注意,您需要 return 一个函数而不是实际值。
我需要使用将在应用程序初始化期间解析的工厂获取配置(使用 APP_INITIALIZER 提供程序)。
export function loadConfig(): () => Promise<Config> {
// return promised config
}
使用 AppModule 提供:
providers: [{
provide: APP_INITIALIZER,
useFactory: loadConfig,
deps: [HttpClient, ConfigService],
multi: true
}]
然后我需要使用该配置数据在另一个 InjectionToken 中注入一些东西,但是如果我使用应用程序初始化期间提供的配置提供特定的 injectiontoken,则该过程在 APP_INITIALIZER 执行之前执行。
export const FORMATS = new InjectionToken<Formats>("formats")
export assignFormat(configService: ConfigService) {
return configService.getFormats(); // Needs to execute after APP_INITIALIZER, not before
}
providers: [{
provide: APP_INITIALIZER,
useFactory: loadConfig,
deps: [HttpClient, ConfigService],
multi: true
}, {
provide: FORMATS,
useFactory: assignFormat,
deps: [ConfigService]
}]
@Injectable({ providedIn: "root" })
export class ConfigService {
constructor() {}
getFormats() {}
}
APP初始化后如何提供injection token?
如果您的 loadConfig
工厂 return 是一个函数而不是实际的 promise:
const loadConfig = (configService: ConfigService) => {
return () =>
new Promise<void>((resolve, reject) => {
// I added the timeout to simulate a delay
setTimeout(() => {
// you might be using a http call to get the config from the server instead.
// Make sure you keep the config that you fetched in the service;
// this way you can inject the service in the next factory function
// and have the config available
configService.config = {
formats: 'formats',
};
resolve();
}, 1000);
});
};
APP_INITIALIZER
的规定与您的代码完全一样:
{
provide: APP_INITIALIZER,
useFactory: loadConfig,
deps: [ConfigService],
multi: true,
},
当您设置下一个注入令牌时,您应该可以使用该配置
{
provide: FORMATS,
useFactory: (configService: ConfigService) => {
// getFormats method must not be async, it needs to return the actual
// formats that were fetched during the app initialization phase
return configService.getFormats();
},
deps: [ConfigService],
},
Angular 中唯一允许的异步工厂是您与 APP_INITIALIZER
injection token 一起使用的那些,但请注意,您需要 return 一个函数而不是实际值。