简体   繁体   English

canActivate返回false并仍然转到屏幕Angular 2

[英]canActivate returns false and still goes to screen Angular 2

I`m having an issue where my canActivate method returns false and still navigates to blocked screen. 我有一个问题,我的canActivate方法返回false并仍然导航到被屏蔽的屏幕。 This issues is only present in Chrome while in IE everything works as intended. 此问题仅出现在Chrome中,而在IE中,一切都按预期工作。 canActivate method looks lke this: canActivate方法看起来像这样:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    return this.getDataFromApi(state.url)
        .toPromise()
        .then(result=> { 
            return result;
        });
}

And getDataFromApi() : 和getDataFromApi():

getDataFromApi(link): Observable<boolean> {
    return this.http.get("api/security")
                    .map(response => {
                        var url = link.replace("/", "");
                        var data = response.json();
                        var privileges = data["privileges"];
                        var currentItem = privileges[url];
                        return currentItem["view"];
                    });
}

Is there any way to resolve this issue in Chrome? 有没有办法在Chrome中解决此问题? Thanks is advance. 谢谢你的提前。

EDIT I've checked in IE, EDGE, Firefox where issue is non present. 编辑我已经在IE,EDGE,Firefox中检查过哪里没有问题。 Chrome works only with developers console open. Chrome仅适用于开发者控制台打开。

This looks like an async problem to me. 这看起来像是一个异步问题。

In you guard you are returning return this.getDataFromApi(state.url) , then when this resolves, you are again trying to return return result; 在你守卫你正在返回return this.getDataFromApi(state.url) ,然后当这个结算时,你再次尝试返回return result; . This will mean your guard is actually returning Before you return result . 这意味着你的后卫实际上已经返回result

I recommend setting up a service for handling login and holding authentication state. 我建议设置一个服务来处理登录和保持身份验证状态。 This service could have properties like isLoggedIn: bool , roles: Role[] etc. 此服务可能具有isLoggedIn: boolroles: Role[]等属性。

Then, in you guard, you simple check with the service if the user isLoggedIn (or something similar). 然后,在您看来,如果用户是isLoggedIn (或类似的东西),您可以使用服务进行简单检查。 If the user is authenticated, you return true, if not, you redirect him or her to a login page. 如果用户已通过身份验证,则返回true,否则,您将其重定向到登录页面。 If the user is logged in, check if the user got the correct roles (with the pre-loaded roles in the service), then return true or false accordingly. 如果用户已登录,请检查用户是否获得了正确的角色(服务中预先加载的角色),然后相应地返回truefalse

Take a look at the official docs , they have a quite good example of this approach. 看看官方文档 ,他们有一个很好的例子来说明这种方法。

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

相关问题 在angular2中,我使用的是canActivate,但是当它返回false时,它仍然会转到该路线 - In angular2 I'm using canActivate but when it returns false it still goes to the route Angular 路由器路由保护 CanActivate 始终返回 false - Angular Router route guard CanActivate always returns false 使用defaultIfEmpty()仍无法在Angular can中激活 - Using defaultIfEmpty() still not emitting in Angular canActivate guard Angular2路由器可以激活任何提供程序false - Angular2 router canActivate no provider for false 当canActivate返回false时,如何重定向另一个组件? - How to redirect another component when canActivate returns false? 返回条件始终为 false 订阅 Angular CanActivate Guard - Return condition always is false into subscribe Angular CanActivate Guard 与keycloak集成时,canActivate在角度4中返回结果之前组件正在加载 - Component is loading before canActivate returns the result in angular 4 when integrated with keycloak 即使Observable返回true,Angular CanActivate在浏览器刷新时也会失败 - Angular CanActivate failing on browser refresh even though observable returns true canActivate返回不需要的值 - canActivate returns unwanted value 了解可以激活并在角度2中路由 - Understanding canactivate and routes in angular 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM