[英]In angular2 I'm using canActivate but when it returns false it still goes to the route
在angular2中,我使用的是canActivate,但是当它返回false时,它仍然会转到该路由。
这是我的代码如下:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
this.isAuthRequired = route.data.isAuthRequired;
this.isCompanyView = route.data.isCompanyView;
return this.init();
}
此代码根据路由数据(例如isAuthRequired等)返回true或false。
这仍然意味着要去那条路线吗? 因为它确实可以,并且只显示蓝屏,没有控制台错误,这很好,但是我希望它重定向到主页,或者至少保留在上一页。
此功能是如何工作的,您有什么建议吗?
确保您从init()方法返回boolean
, promise
或observar
。 从init()返回true将导航到选定的路由。 返回false将阻止从当前屏幕进行页面导航。
像下面这样:
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();
}
希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.