i have a problem during use httpclient in angular7 Route guards service. since i have to get a result from a background server to judge if pass or not. but httpclient service is async,so the variable canPass is always false,how to deal with the problem? any opinion will be appreciated. here is my code:
judgePass(route: string): Observable<CommonResult> {
const url = `${this.baseUrl}/check`;
const formData = new FormData();
formData.append('url', route);
return this.httpClient.post<CommonResult>(url, formData, {});
}
canPass: boolean = false;
canActivate(route: ActivatedRouteSnapshot, state:
RouterStateSnapshot):boolean {
this.authService.judgePass(state.url).subscribe(res => {
let code = res['code'];
if (code == 0) {
this.canPass = true;
} else {
this.message.create('error', res.message);
this.canPass = false;
}
});
return this.canPass;
}