Angular2 路由:如何从服务而不是 routerLink 导航到子视图?

mth*_*led 3 angular2-routing

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

this.router.navigate(['./details']); //Error: Cannot match any routes: 'details'
Run Code Online (Sandbox Code Playgroud)

但是在我使用 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 },
        ]
      }
    ]
  }
];
Run Code Online (Sandbox Code Playgroud)

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

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

Vic*_*hin 6

第一的

import { ActivatedRoute } from '@angular/router';
Run Code Online (Sandbox Code Playgroud)

然后在你的组件中

constructor(private route: ActivatedRoute) {}
Run Code Online (Sandbox Code Playgroud)

然后

this.router.navigate(['./details'], { relativeTo: this.route });
Run Code Online (Sandbox Code Playgroud)