Angular: 使用守卫防止没有参数的URL

huangapple go评论60阅读模式
英文:

Angular: Prevent URL without param using a guard

问题

我想要防止用户将URL从/dashboard/1234-12更改为/dashboard以避免错误,所以我希望在用户输入/dashboard但没有id时重定向到/home

我有这个路由

{
  path: 'dashboard/:id',
  component: DashboardComponent,
  canActivate: [CanActivateViaAuthGuard, CheckVidGuard],
},

以及这个CheckVidGuard

canActivate(
  route: ActivatedRouteSnapshot,
  state: RouterStateSnapshot
): boolean {
  const vid = route?.params?.vid;

  if (!vid) {
    this.router.navigate(['/home']);
    return false;
  }

  return true;
}

我的Dashboard组件只获取这个id并执行一些操作:

ngOnInit() {
  this.id = this.activatedRoute.snapshot.paramMap?.get('id');
}

但是,每次我尝试访问/dashboard(没有id参数)以重定向到/home时,我都会收到此错误Error: NG04002: Cannot match any routes. URL Segment: 'dashboard'

我知道没有定义不带id的路由,但守卫似乎不起作用。

我尝试添加这个路由,它可以工作,但我不想为此添加其他路由。

{
  path: 'dashboard',
  component: DashboardComponent,
  canActivate: [CanActivateViaAuthGuard],
}

使用Resolver结果相同,不起作用:

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): string {
  const vid = route.params['vid'];
  if (!vid) {
    this.router.navigate(['/home/dashboard']);
    return null;
  }
  return vid;
}
英文:

I want to avoid user change the URL from /dashboard/1234-12 to /dashboard to prevent an error, so I want to redirect to /home when user type /dashboard without the id.

I have this route:

{
  path: 'dashboard/:id',
  component: DashboardComponent,
  canActivate: [CanActivateViaAuthGuard, CheckVidGuard],
},

And this CheckVidGuard:

canActivate(
  route: ActivatedRouteSnapshot,
  state: RouterStateSnapshot
): boolean {
  const vid = route?.params?.vid;

  if (!vid) {
    this.router.navigate(['/home']);
    return false;
  }

  return true;
}

My Dashboard component only get this id and do some things:

ngOnInit() {
  this.id = this.activatedRoute.snapshot.paramMap?.get('id');
}

But every time I tried to go to /dashboard (without the id param) to get redirected to /home I got this error Error: NG04002: Cannot match any routes. URL Segment: 'dashboard'

I know that there is no route defined without the id but the guard seems to not work.

I tried adding this route and it works, but I don't want to add other route for this.

{
  path: 'dashboard',
  component: DashboardComponent,
  canActivate: [CanActivateViaAuthGuard],
},

With a Resolver the result is the same, don't work:

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): string {
  const vid = route.params['vid'];
  if (!vid) {
    this.router.navigate(['/home/dashboard']);
    return null;
  }
  return vid;
}

答案1

得分: 1

根据 https://angular.io/guide/router#route-order

你可以在 dashboard/{id} 路由之后添加一个仪表板路由,将其重定向到主页。

或者在配置的末尾添加一个通配符路由,以重定向到主页。

英文:

According to https://angular.io/guide/router#route-order

You could just add a dashboard route after the dashboard/{id} route with a rediract to home.

Or have a wildcard route at the and of the config to redirect to home

答案2

得分: 0

Ivan Tarskich所说,我终于使用这个通用路由实现了通配符解决方案:

{
  path: '**',
  redirectTo: '/home',
},

不确定是否是更合适的解决方案,但它有效。

英文:

As Ivan Tarskich said, I have finally implemented the wildcard solution using this generic route:

{
  path: '**',
  redirectTo: '/home',
},

Not sure if it is the more appropriate solution but it works.

huangapple
  • 本文由 发表于 2023年2月16日 19:35:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定