В Angular 6 у меня есть ConfigService
, который требует, чтобы конструктор закончил работать хорошо:
import { HttpClient, HttpHeaders, HttpErrorResponse } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable, Subscribable, Subscription } from "rxjs";
@Injectable()
export class ConfigService {
public configKeys: any;
constructor(private http: HttpClient) {
this.http.get('config/config.json').subscribe(
data => {
this.configKeys = data;
},
(err: HttpErrorResponse) => {
console.log (err.message);
}
);
}
getDocBaseRoute() :string{
return this.configKeys.docBaseRoute;
}
getAuthenticationBaseRoute() : Subscription {
return this.configKeys.authenticationBaseRoute;
}
}
При попытке вызвать метод службы возникает проблема:
this.baseUrl = this.configservice.getAuthenticationBaseRoute();
На самом деле в данный момент configKeys
не определено, поэтому return this.configKeys.authenticationBaseRoute;
выдает ошибку.
Не могли бы вы помочь мне узнать, как я могу быть уверен, что конструктор завершен до вызова методов службы?