假设路由守卫有这种场景
需要使用observable同时发送多个Http 请求,判断request2返回的数据中是否存在request1返回的数据
使用async await
export class testAuth implements CanActivate { username = localStorage.getItem("username"); EmployeeNo: any; constructor( private dpaService: DpaService, private testService: TestService, ) { } async canActivate( next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean | UrlTree> { try{ if(this.username){ //forkJoin同时发送多个http请求 return await forkJoin([this.dpaService.getUserByEmail(this.username),this.testService.queryAll()]) .toPromise().then((results:any)=>{ //results为多个请求返回的结果数组 this.EmployeeNo = results[0][0].EmployeeNo; return results[1].filter((element: any)=>element.emplId === this.EmployeeNo).length>0; }); } else{ return false; } } catch(err){ return false; } } }