gpt4 book ai didi

angular2-routing - Angular2 路由 : How to navigate to a child view from a service instead of routerLink?

转载 作者:行者123 更新时间:2023-12-05 01:45:19 24 4
gpt4 key购买 nike

尝试使用组件中的路由器对象通过代码导航到 subview 时遇到问题:

this.router.navigate(['./details']); //Error: Cannot match any routes: 'details'

但在我使用 routerLink 的模板中,它有效: 考验我我需要做同样的事情,但在组件内使用 router.navigate 方法。我的组件显示在 routerOutlet 的另一个组件中,我有以下子路由配置:

export const HOME_ROUTES: Routes = [
{
path: 'home',
component: HomeComponent,
children: [{path: '',outlet: 'route1',component: DashboardComponent,pathMatch: 'full'},
{
path: 'dashboard',
outlet: 'route1',
component: DashboardComponent
},
{
path: 'tasks',
outlet: 'route1',
component: TasksComponent,
children: [
{ path: '',component: TasksListComponent,pathMatch: 'full'},
{ path: 'list',component: TasksListComponent},
{ path: 'details', component: TasksDetailsComponent },
{ path: 'view', component: TasksListComponent },
]
}
]
}
];

我正在尝试显示 TasksListComponent 中的 TasksDetailsComponent,我的相对 URL 是“http://localhost:8000/home/(route1:tasks)

非常感谢任何帮助非常感谢

最佳答案

首先

import { ActivatedRoute } from '@angular/router';

然后在你的组件中

constructor(private route: ActivatedRoute) {}

然后

this.router.navigate(['./details'], { relativeTo: this.route });

关于angular2-routing - Angular2 路由 : How to navigate to a child view from a service instead of routerLink?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41991647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com