Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用CanActivate时重定向到特定页面_Angular_Canactivate - Fatal编程技术网

Angular 使用CanActivate时重定向到特定页面

Angular 使用CanActivate时重定向到特定页面,angular,canactivate,Angular,Canactivate,在我的应用程序中,当用户尝试查看页面“主页”时,他将被重定向到登录页面(如果他尚未登录)。如果用户已经登录,或者在他登录之后,我想将他重定向到“主页”页面,或者重定向到登录之前他试图查看的任何页面 这是我的canActivate模块。如何将用户重定向到他来自的页面,而不是返回true?是否可以在canActivate方法中执行此操作 export class RouteGuardService implements CanActivate{ constructor(private rout

在我的应用程序中,当用户尝试查看页面“主页”时,他将被重定向到登录页面(如果他尚未登录)。如果用户已经登录,或者在他登录之后,我想将他重定向到“主页”页面,或者重定向到登录之前他试图查看的任何页面

这是我的canActivate模块。如何将用户重定向到他来自的页面,而不是返回true?是否可以在canActivate方法中执行此操作

export class RouteGuardService implements CanActivate{

  constructor(private router:Router, private authService:AuthService) { }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> {
    return this.authService.user.pipe(
      take(1),
      map(user=>{
        if(user && user.token){              
          return true;
        }
        return this.router.createUrlTree(["/login"]);
      })
    );
  }
}
导出类RouteGuardService实现CanActivate{
构造函数(专用路由器:路由器,专用authService:authService){}
canActivate(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot):布尔值| UrlTree |可观察|承诺{
返回this.authService.user.pipe(
以(1)为例,
映射(用户=>{
if(user&&user.token){
返回true;
}
返回此.router.createUrlTree([“/login”]);
})
);
}
}

可以激活保护链接并允许有条件地访问,例如在用户身份验证应用程序中。链接将由CanActivate保护,只有在登录后才能访问

因此,在代码中,只需删除一行即可

 if(user && user.token){
      return true; //if the user is logged in you only need to return true, that's it
    }
最终解决方案:

  export class RouteGuardService implements CanActivate{

  constructor(private router:Router, private authService:AuthService) { }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> {
    return this.authService.user.pipe(
      take(1),
      map(user=>{
        if(user && user.token){
          return true;
        }
         let url: string = state.url; // current url user trying to go to
         this.authService.setRedirectUrl(url); // store this url user is trying in authservice
         this.router.navigate(['login']);// navigate to login
         return false;
      })
    );
  }
}
用户成功登录后,在AuthServiceFile中:

 // store the URL so we can redirect after logging in
  private redirectUrl: string; 
  if (this.redirectUrl) {
    this.router.navigate([this.redirectUrl]);
    this.redirectUrl = null;
  }
setRedirectUrl(url){
  this.redirectUrl =  url
}

 

试试这个嗨,谢谢你帮我。我需要的是一种方法来重定向用户从他来自的页面。例如,如果用户尝试查看“主页”页面,如果他没有登录,则会重定向到“登录”页面。在他登录后,我想将他重定向到他来自的页面(例如“主页”)。现在,在用户登录后,他继续停留在登录页面上。我已经为您提到的场景添加了步骤,请检查我的更新答案。谢谢,伙计,您的解决方案非常有效:)
 // store the URL so we can redirect after logging in
  private redirectUrl: string; 
  if (this.redirectUrl) {
    this.router.navigate([this.redirectUrl]);
    this.redirectUrl = null;
  }
setRedirectUrl(url){
  this.redirectUrl =  url
}