Мне нужно установить свойство base href при загрузке приложения.все работало замечательно, пока нам не пришлось добавить проверку IP и установить базовый href на основе ответа от них.Вот мой сервис, который предоставляет информацию об основной конфигурации приложения на основе href:
import { Injectable, Injector, Inject, PLATFORM_ID } from "@angular/core";
import { isPlatformServer } from "@angular/common";
import { REQUEST } from "@nguniversal/express-engine/tokens";
import { Http } from "@angular/http";
@Injectable()
export class AppConfigService {
public config: any = {
appBaseHref: "",
country: "",
currency: ""
};
parseURL;
code;
constructor(
private _injector: Injector,
private http: Http,
@Inject(PLATFORM_ID) private platformId: Object
) {
if (isPlatformServer(this.platformId)) {
// -> server rendered
let request = this._injector.get(REQUEST);
this.parseURL = request.originalUrl.split("/");
this.code = this.parseURL[1];
} else {
this.parseURL = location.href.split("/");
this.code = this.parseURL[3];
}
}
getAsync() {
return new Promise((resolve, reject) => {
if (this.code == "") {
return new Promise((resolve,reject) => {
this.http
.get("https://ipinfo.io/json?token=xoxoxoxoxox")
.map(res => res.json())
.subscribe(response => {
console.log(response);
if (response.country == "PL" || response.country == "pl") {
this.config.appBaseHref = "/pl";
this.config.country = "PL";
this.config.currency = "PLN";
} else {
this.config.appBaseHref = "/gb";
this.config.country = "GB";
this.config.currency = "GBP";
}
resolve(this.config.appBaseHref);
});
})
} else if (this.code == "pl") {
this.config.appBaseHref = "/pl";
this.config.country = "PL";
this.config.currency = "PLN";
} else if (this.code == "uae") {
this.config.appBaseHref = "/gb";
this.config.country = "GB";
this.config.currency = "GBP";
} else {
this.config.appBaseHref = "/pl";
this.config.country = "PL";
this.config.currency = "PLN";
}
resolve(this.config.appBaseHref);
});
}}
Я использую его в app.module следующим образом
...
providers: [
AppConfigService,
{
provide: APP_BASE_HREF,
useFactory: async (config: AppConfigService) => {
return await config.getAsync();
},
deps: [AppConfigService]
},
...
В результате я получаюошибка в консоли url.replace is not a function
Похоже, что он не ждет, пока обещание будет перезапущено, и пытается установить APP_BASE_HREF ранее.Я использую неправильный провайдер - я попытался добавить APP_INITIALIZER, но из моего наблюдения APP_BASE_HREF не дождался, пока инициализатор также будет разрешен.Любая помощь высоко ценится