简体   繁体   中英

Angular Router Navigating to parent node

Given this paths tree:

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,
                }]
              }]
          },

        ]
      },
    ]
  },
];

Navigating from admin/course/manage/1/unit/1 to parent manage/:id is not working by both below conventional navigating ways.

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

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

Here is a StackBlitz

Since you are on a children path '' of route unit/:unitId I will assume it's because your path is empty in the routes.

Relating to parent path node instead of the current empty one will make this work for you

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM