Angular 路由器:如何根据路由器链接的一部分重定向到组件
Angular router : how to redirect to a component based on a part of the routerlink
在我的 Angular 应用程序中
我有这个路由文件:
const routes: Routes = [
{
path: "",
children: [
{
path: "",
redirectTo: "/my-default-page",
pathMatch: "full"
},
{
path: "reset_password",
component: ResetPassword,
canActivate: [MyAuthGuard],
children: [{ path: "**", redirectTo: "/reset_password" }]
}
]
}
}
];
我不想通过重定向,当路由 : 会是这样的 :
/reset_password/blabalabla
它不会重定向我,只是加载组件 ResetPassword 并保持 url 原样
建议 ?
使用路由参数。然后你不需要任何子路由。
{
path: "reset_password/:id",
component: ResetPassword,
canActivate: [MyAuthGuard]
}
您的 link 将保持不变,如果您愿意,可以在 ResetPassword 组件
中使用以下代码从 link 检索此参数
constructor(private route: ActivatedRoute){}
ngOnInit():void{
const id = this.route.snapshot.paramMap.get('id');
}
在我的 Angular 应用程序中
我有这个路由文件:
const routes: Routes = [
{
path: "",
children: [
{
path: "",
redirectTo: "/my-default-page",
pathMatch: "full"
},
{
path: "reset_password",
component: ResetPassword,
canActivate: [MyAuthGuard],
children: [{ path: "**", redirectTo: "/reset_password" }]
}
]
}
}
];
我不想通过重定向,当路由 : 会是这样的 :
/reset_password/blabalabla
它不会重定向我,只是加载组件 ResetPassword 并保持 url 原样
建议 ?
使用路由参数。然后你不需要任何子路由。
{
path: "reset_password/:id",
component: ResetPassword,
canActivate: [MyAuthGuard]
}
您的 link 将保持不变,如果您愿意,可以在 ResetPassword 组件
中使用以下代码从 link 检索此参数constructor(private route: ActivatedRoute){}
ngOnInit():void{
const id = this.route.snapshot.paramMap.get('id');
}