【问题标题】:Angular2 Routing: How to navigate to a child view from a service instead of routerLink?Angular2路由:如何从服务而不是routerLink导航到子视图?
【发布时间】:2017-02-01 23:18:47
【问题描述】:

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

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)

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

【问题讨论】:

    标签: angular2-routing


    【解决方案1】:

    第一

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

    然后在你的组件中

    constructor(private route: ActivatedRoute) {}
    

    然后

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

    【讨论】:

    • @mthkhaled 如果对您有用,请随时接受此答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2017-10-10
    • 1970-01-01
    • 2017-02-26
    • 2017-07-11
    • 2018-06-08
    • 1970-01-01
    相关资源
    最近更新 更多