是否可以通过从服务中的http调用获取值来动态地将APP_BASE_HREF设置为角度6?

时间:2018-10-31 12:26:56

标签: angular6 angular-universal ssr

这是来自app.module.ts文件的代码

{
  provide: APP_BASE_HREF,
  useFactory: referalCodeValidator,
  deps: [ValidatorService]
}

app.module.ts文件本身定义了一个ReferalCodeValidator函数

export function referalCodeValidator(common: ValidatorService) {
  let path = location.pathname;
  let refCode = path.split("/", 2);
  return common.validateReferralCode(refCode[1]);
}

验证器服务如下:

import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { environment } from "../../environments/environment";
import { HttpClient } from "@angular/common/http";
import { tap, catchError } from "rxjs/operators";

@Injectable()
export class ValidatorService {
  appBaseHref: any;
  constructor(private http: HttpClient) {}

  validateReferralCode(refCode): Observable<any> {
    if (refCode != "" || refCode != null) {
      this.appBaseHref = null;
      return this.http
        .get(someurl, {
          params: {
            referenceCode: refCode
          }
        })

        .pipe(catchError((error, caught) => {
          console.log(error);
          throw error.error;
        }) as any);
      }
    }
}

0 个答案:

没有答案