我正在尝试访问 AppModule(下面的 OidcConfigService)内的工厂函数中的一些值提供程序。通常,在组件内部,我可以只使用@Inject('PROVIDER_NAME') 通过构造函数注入来获取我试图获取的值。在这种情况下,它只是一个工厂函数,所以我没有构造函数来获取我的值。您可以看到我尝试在 OidcConfigService 提供程序中获取值,但这些只是失败的尝试。
AppModule(底部的功能 configureAuth 是我试图发送值的地方)
import { HttpClientModule } from '@angular/common/http';
import { APP_INITIALIZER, NgModule, Input, Inject } from '@angular/core';
import { FormsModule } from '@angular/forms';
//Angular modules
import { BrowserModule } from '@angular/platform-browser';
//Third-party modules
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { AuthModule, OidcConfigService, LogLevel, PublicEventsService, EventTypes } from 'angular-auth-oidc-client';
import { MDBBootstrapModule } from 'angular-bootstrap-md';
import { ConvertDatetimeToLocalPipe } from 'src/app/pipes/datetime/convert-datetime-to-local-pipe';
import { AppRoutingModule } from './app-routing.module';
//Components
<<LIST OF COMPONENTS>>
@NgModule({
declarations: [
<<LIST OF COMPONENTS>>
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
FormsModule,
NgbModule,
AuthModule.forRoot(),
CommonModule,
MDBBootstrapModule.forRoot()
],
providers: [
RequestService,
ConvertDatetimeToLocalPipe,
OidcConfigService,
{
provide: [APP_INITIALIZER, { provide: AUTH_URL, useValue: AppConfig }],
useFactory: configureAuth,
deps: [OidcConfigService, @Inject('AUTH_URL')],
multi: true
}
],
bootstrap: [AppComponent]
})
export class AppModule {
constructor(private readonly eventService: PublicEventsService) {
this.eventService
.registerForEvents()
.pipe(filter((notification) => notification.type === EventTypes.ConfigLoaded))
.subscribe((config) => {
console.log('ConfigLoaded', config);
});
}
}
export function configureAuth(oidcConfigService: OidcConfigService) {
return () =>
oidcConfigService.withConfig({
//stsServer: this._authUrl,
stsServer: 'http://localhost:5999',
redirectUrl: `${window.location.origin}/postloginloading`,
clientId: 'admin_web_app_client',
responseType: 'code',
scope: 'openid profile role ReviewApi UserApi',
postLogoutRedirectUri: window.location.origin,
forbiddenRoute: '/forbidden',
unauthorizedRoute: '/unauthorized',
silentRenew: true,
silentRenewUrl: `${window.location.origin}/silent-renew.html`,
historyCleanupOff: true,
autoUserinfo: true,
logLevel: LogLevel.Warn,
maxIdTokenIatOffsetAllowedInSeconds: 10,
storage: localStorage //If not working, change back to sessionStorage
});
}
main.ts (这些是我试图访问的值)
const providers = [
{ provide: 'BASE_URL', useFactory: getBaseUrl, deps: [] },
{ provide: 'AUTH_URL', useValue: 'http://localhost:5999' },
{ provide: 'REVIEWAPI_URL', useValue: 'http://localhost:5101' },
{ provide: 'USERAPI_URL', useValue: 'http://localhost:5105' }
];
platformBrowserDynamic(providers).bootstrapModule(AppModule)
.catch(err => console.error(err));
提前致谢!