APP_BASE_HREF是否可以等待承诺解决

时间:2018-10-12 08:52:54

标签: angular angular-universal

我需要在应用程序引导程序上设置基本href属性。一切正常,直到我们不得不添加IP检查并根据来自ther的响应设置基本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不要等到初始化程序也解决了。 任何帮助表示赞赏

0 个答案:

没有答案