Angular2 trigger CanActivate while page is active

核能气质少年 提交于 2019-12-05 16:07:08

you may add data with your routes, which you may use later to check if authentication is required for that route or not,

something like below,

{
  path: ..., 
  component: ..., 
  canActivate: [CanActivateGuard], 
  data: { authRequired: true }
}

in the logout trigger, you can inject ActivatedRoute to check on the activated routes data to determine if you want to send the user to login page or not,

constructor(route: ActivatedRoute) {}
...
someCallBack(){
  if(!!this.route.snapshot.data 
     && this.route.snapshot.data.authRequired == true){

     // redirect to login page

  }
}

Hope this helps!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!