这是来自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);
}
}
}