AngularRoutercanActivate/redirectTo造成无限循环
创始人
2024-10-29 06:00:42
0
  1. 确认在路由守卫中使用了正确的 redirectTo 形式。可使用带有相对路径的 redirectTo,而不是绝对路径。例如:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { if (condition) { return true; } else { return this.router.createUrlTree(['/login']); } }

  1. 确认在路由守卫中使用了正确的语法。需要为 RedirectTo 返回一个 UrlTree 对象。示例如下:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): UrlTree { if (condition) { return true; } else { return this.router.parseUrl('/login'); } }

  1. 避免在 canActivate 守卫中使用 redirectTo。可选择在路由导航事件中处理。示例代码如下:

import { Injectable } from '@angular/core'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, NavigationExtras } from '@angular/router'; import { AuthService } from './auth-service.service'; import { Observable } from 'rxjs/Observable';

@Injectable() export class AuthGuard implements CanActivate {

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

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | Promise | boolean { if (this.authService.isLoggedIn()) { return true; }

const navigationExtras: NavigationExtras = {
  queryParams: { 'redirectUrl': state.url }
};

this.router.navigate(['/login'], navigationExtras);
return false;

} }

相关内容

热门资讯

透视秘籍!uupoker有透视... 透视秘籍!uupoker有透视吗(AApoker俱乐部)本来是有辅助软件(哔哩哔哩)1、uupoke...
透视妙计!wepoker辅助透... 透视妙计!wepoker辅助透视软件(WePoKer看底牌)一直真的是有辅助教程(哔哩哔哩)该软件可...
透视阶段!pokernow辅助... 透视阶段!pokernow辅助控制(WePoKer教程)总是真的是有辅助脚本(哔哩哔哩)1、全新机制...
透视妙计!aapoker怎么拿... 透视妙计!aapoker怎么拿好牌(WePoKer黑科技)一贯是真的有辅助软件(哔哩哔哩)1、aap...
透视步骤!wpk辅助软件(We... 透视步骤!wpk辅助软件(WePoKer开辅助)真是是有辅助方法(哔哩哔哩)1、点击下载安装,wpk...
透视步骤!约局吧app有挂吗(... 透视步骤!约局吧app有挂吗(WePoKer养号)果然是真的有辅助神器(哔哩哔哩)1、完成约局吧ap...
透视妙招!wepoker透视脚... 透视妙招!wepoker透视脚本网页(WePoKer软件)一贯存在有辅助脚本(哔哩哔哩)所有人都在同...
透视操作!wejoker免费脚... 透视操作!wejoker免费脚本(AApoker方法)真是是真的有辅助脚本(哔哩哔哩)小薇(辅助器软...
透视指引!wepoker有没有... 透视指引!wepoker有没有挂(WePoKer钻石)确实真的有辅助脚本(哔哩哔哩)1)wepoke...
透视策略!hhpoker软件可... 透视策略!hhpoker软件可以玩吗(AApoker安装包)一直真的有辅助软件(哔哩哔哩)hhpok...