简体   繁体   English

Angular Guard - 如何让 canActivate 等待一个值?

[英]Angular Guard - how to make canActivate wait for a value?

I have a guard that depends on a client.我有一个依赖客户的警卫。 The guard isn't working as expected because the client is not ready by the time the guard is hit.守卫没有按预期工作,因为客户端在守卫被击中时还没有准备好。 How do I get the guard to wait until the client is ready?我如何让警卫等到客户准备好?

public canActivate() {
   if (this.ffService.getFlag(FLAG)) {
      // KEEPS HITTING HERE BECAUSE CLIENT IS NOT READY
      return observableOf(true);
   }

   // NEEDS TO MAKE IT TO THE LOGIC BELOW HERE
}

In the feature flag service:在功能标志服务中:

getFlag(flag) {
   if (!this.ffClient.isReady()) {
       // KEEPS HITTING HERE
       return false;
   }
   return this.ffClient.find(flag);
}

I have tried adding retry logic to the keep checking if the isReady(), but it's just making the guard not load anything.我已经尝试添加重试逻辑以继续检查 isReady() 是否已就绪,但这只是让守卫不加载任何东西。

Try this:尝试这个:

public canActivate(): Observable<boolean> {
   return this.ffservice.getFlag(FLAG).pipe(
     filter(flag => flag), // will only pass if flag exists and(or if it's an obj) it's true
     mapTo(true)
   );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM