我使用的是角度路线,通过组件导航。当用户登录时,它们被导航到dashboard/:id,然后看到dashboard/123/projects等。问题是,当用户只导航到/dashboard时,我想默认地将它们重定向到/dashboard/:id。但是,当我这样做时:{ path:"dashboard", redirectTo:"dashboard/:id/projects", pathMatch:"full"}它通过一个错误说:id未定义,并且我无法导航到从/dashboard到当前登录/dashboard/123/projects的用户。
我的Routes.modules.ts代码
const routes: Routes = [{
path: "",
redirectTo: "/login",
pathMatch: "full"
},
{
path: "login",
component: LoginComponent
},
{
path: "register",
component: RegisterComponent
},
//the problem goes here
{
path: "dashboard",
redirectTo: "", //How to redirect to dashboard/:id ?
pathMatch: "full"
},
{
path: "dashboard/:id",
component: DashboardComponent,
children: [{
path: "",
redirectTo: "projects",
pathMatch: "full"
},
{
path: "projects",
component: ProjectsComponent
},
{
path: "archived",
component: ArchivedProjectsComponent
},
{
path: "projects/:id",
component: SingleProjectComponent,
children: [{
path: "",
redirectTo: "plans",
pathMatch: "full"
},
{
path: "plans",
component: PlansComponent
},
{
path: "tasks",
component: TasksComponent
},
{
path: "photos",
component: PhotosComponent
},
{
path: "files",
component: FilesComponent
},
{
path: "people",
component: PeopleComponent
},
{
path: "settings",
component: SettingsComponent
},
{
path: "trash",
component: TrashComponent
},
{
path: "**",
redirectTo: "plans",
pathMatch: "full"
}
]
},
{
path: "**",
redirectTo: "projects",
pathMatch: "full"
}
]
},
{
path: "**",
component: PageNotFoundComponent
},
];
发布于 2019-05-15 12:21:26
您可以通过您的登录组件来实现这一点。成功登录后,用户重定向到子路由。
从有效负载或API调用中获取用户的id取决于您的结构,并将其传递给路由。
this.router.navigate(['/dashboard/' + id + '/projects']);this.router是Router的一个实例。
发布于 2019-05-15 12:25:07
我认为您需要将":id“的值存储在类似于请求之间的本地存储中的某个地方,即。存储最后一个"id“。
在重定向中提供任意值,然后在DashboardComponent中捕获这个值。查找9999 (或其他什么),而不是替换本地存储的值。
ngOnInit() {
localStorage.getItem('id'))
}{
path: "dashboard",
redirectTo: "dashboard/99999", //How to redirect to dashboard/:id ?
pathMatch: "full"
}https://stackoverflow.com/questions/56149125
复制相似问题