با تغییر کلاس سرویس AppConfigService
به شکل زیر :
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
@Injectable()
export class AppConfigService {
private config: IAppConfig;
constructor(private http: HttpClient) { }
loadClientConfig(): Promise<any> {
return this.http.get<IAppConfig>("assets/client-config.json")
.toPromise()
.then(config => {
this.config = config;
console.log("Config", this.config);
})
.catch(err => {
return Promise.reject(err);
});
}
get configuration(): IAppConfig {
if (!this.config) {
throw new Error("Attempted to access configuration property before configuration data was loaded.");
}
return this.config;
}
}
export interface IAppConfig {
apiEndpoint: string;
loginPath: string;
logoutPath: string;
refreshTokenPath: string;
accessTokenObjectKey: string;
refreshTokenObjectKey: string;
adminRoleName: string;
}
و تغییر ماژول CoreModule
به شکل زیر :
import { NgModule, Optional, SkipSelf, APP_INITIALIZER } from "@angular/core";
import { CommonModule } from "@angular/common";
import { RouterModule } from "@angular/router";
import { HTTP_INTERCEPTORS } from "@angular/common/http";
// import RxJs needed operators only once
import "./services/rxjs-operators";
import { HeaderComponent } from "./component/header/header.component";
import { AuthGuard } from "./services/auth.guard";
import { AuthInterceptor } from "./services/auth.interceptor";
import { AuthService } from "./services/auth.service";
import { AppConfigService } from "./services/app-config.service";
import { BrowserStorageService } from "./services/browser-storage.service";
@NgModule({
imports: [CommonModule, RouterModule],
exports: [
// components that are used in app.component.ts will be listed here.
HeaderComponent
],
declarations: [
// components that are used in app.component.ts will be listed here.
HeaderComponent
],
providers: [
// global singleton services of the whole app will be listed here.
BrowserStorageService,
AppConfigService,
AuthService,
AuthGuard,
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true
},
{
provide: APP_INITIALIZER,
useFactory: (config: AppConfigService) => () => config.loadClientConfig(),
deps: [AppConfigService ],
multi: true
}
]
})
export class CoreModule {
constructor( @Optional() @SkipSelf() core: CoreModule) {
if (core) {
throw new Error("CoreModule should be imported ONLY in AppModule.");
}
}
}
با خطای زیر مواجه شدم لطفا راهنمایی بفرمائید :
Error: Provider parse errors:
Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1