我正在创建微前端项目。在令牌注入方面遇到了问题。
我有一个库项目,被mfe1使用,然后加载到shell应用程序中。
库模块
@NgModule({
declarations: [
EclatechCoreComponent
],
imports: [
],
exports: [
EclatechCoreComponent
]
})
export class EclatechCoreModule {
public static forRoot(config: Environment): ModuleWithProviders<EclatechCoreModule> {
return {
ngModule: EclatechCoreModule,
providers: [
{
provide: ENVIRONMENT,
useValue: config
}
]
}
}
}
来自 mfe1 的信息:
在模块导入中。
@NgModule({
declarations: [SetupComponent],
imports: [
CommonModule,
RouterModule.forChild([
{
path: '',
component: SetupComponent,
}
]),
EclatechCoreModule.forRoot(config)
],
exports:[
SetupComponent
]
})
export class SetupModule { }
现在,当我加载
mfe1
时,配置正确加载并且一切正常,但是当我通过 shell
应用程序加载 mfe1
时,它会返回错误。
Shell 应用程序错误
core.js:6479 ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(SetupModule)[BusinessService -> BusinessService -> ConfigService -> ConfigService -> InjectionToken ENVIRONMENT -> InjectionToken ENVIRONMENT -> InjectionToken ENVIRONMENT]:
NullInjectorError: No provider for InjectionToken ENVIRONMENT!
环境
export interface Environment {
gatewayUrl: string;
orgServiceUrl: string;
client_id: string;
}
环境
import { InjectionToken } from "@angular/core";
import { Environment } from "./environment.interface";
export const ENVIRONMENT : InjectionToken<Environment> = new InjectionToken<Environment>('ENVIRONMENT');