首页 > 解决方案 > Route guard 中的多个 observable 但只等待一个 - Angular 5

问题描述

我的应用程序中有一个路由守卫,如果有人成功,我需要检查两个可观察对象,我应该允许路由或阻止。

路线守卫:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): any {
return (this.isValid().map(valid => {
            console.log(valid);
            return valid;
        }) || this.isValidFromApi().map(valid => {
            console.log(valid);
            return valid;
        }));
} //how to achieve something like this
}
private isValid() {
        return this.service.details.map(resp => {
            console.log(resp);
            if (resp.data) {
                return true;
            } else {
                return false;
            }
        });
    }

    private isValidFromApi() {
        return this.service.getKeys().map((response) => {
            console.log(response);
            if (response) {
                return true;
            } else {
return false;
            });
        }

因此,如果 isValid() 或 isValidFromApi() 中的任何一个返回 true,则路由保护 canActivate 应该处于活动状态。如何做到这一点?我不想等待这两个调用完成,因为 this.service.details 实际上是一个 BehaviorSubject,它包含 this.service.getKeys() 的响应。

有没有其他方法可以实现这一目标?

提前致谢

标签: angularangular5observableangular-route-guards

解决方案


推荐阅读