简体   繁体   English

在angular2中,我使用的是canActivate,但是当它返回false时,它仍然会转到该路线

[英]In angular2 I'm using canActivate but when it returns false it still goes to the route

In angular2 I'm using canActivate but when it returns false it still goes to the route. 在angular2中,我使用的是canActivate,但是当它返回false时,它仍然会转到该路由。

Here my code below: 这是我的代码如下:

 canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
        this.isAuthRequired = route.data.isAuthRequired;
        this.isCompanyView = route.data.isCompanyView;

        return this.init();  
  }

This code returns true or false depending on the routes data eg isAuthRequired etc. 此代码根据路由数据(例如isAuthRequired等)返回true或false。

Is this still meant to go to the route? 这仍然意味着要去那条路线吗? because it does and just shows a blue screen with no console errors which is good but I'm wanting it to redirect to homepage or atleast stay on the previous page. 因为它确实可以,并且只显示蓝屏,没有控制台错误,这很好,但是我希望它重定向到主页,或者至少保留在上一页。

How is this feature mean't to work and do you have any suggestions. 此功能是如何工作的,您有什么建议吗?

Make sure you are returning a boolean or promise or observar from the init() method. 确保您从init()方法返回booleanpromiseobservar Returning true from init() will navigate to the selected routing. 从init()返回true将导航到选定的路由。 Returning false will prevent page navigation from the current screen. 返回false将阻止从当前屏幕进行页面导航。

something like the below :- 像下面这样:

init(): boolean{
    /* your application logic */

    return true;
}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    this.isAuthRequired = route.data.isAuthRequired;
    this.isCompanyView = route.data.isCompanyView;

    return this.init();  
}

I hope this can help you. 希望对您有所帮助。

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

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