英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论