Angular Router导航到父节点

时间:2019-02-22 16:53:49

标签: angular

给出此路径树:

const routes: Routes = [
  {
    path: 'admin',
    children: [
      { path: '', component: AdminComponent },
      {
        path: 'course',
        children: [
          {
            path: 'manage/:_id',
            children: [
              {
                path: '', component: CourseComponent,
              },
              {
                path: 'unit/:unitId',
                children: [{
                  path: '',
                  component: UnitComponent,
                }]
              }]
          },

        ]
      },
    ]
  },
];

admin/course/manage/1/unit/1到父级manage /:id的导航在两种常规导航方式下均无法正常工作。

<a routerLink="../../">router link Back</a>
<button (click)="back()">Imperative Back</button>

  protected back() {
    this.router.navigate(['../../'], { relativeTo: this.route, fragment: 'coolFrag' });
  }

这里是StackBlitz

1 个答案:

答案 0 :(得分:1)

由于您位于路径''的子路径unit/:unitId上,所以我认为这是因为您的路径在路径中为空。

与父路径节点(而不是当前的空节点)相关,它将为您工作

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