Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 navigate

Considering I'm on route

/parentOne/param1/param2/child

Is there any way for me to navigate to this another route, without passing the entire path (/param1/param2/child)?

/parentTwo/param1/param2/child

The solution I have today:

const path = this.router.url.replace(regex, 'parentTwo');
this.router.navigate([path]);

The solution I wish I could implement:

this.router.navigate(['parentTwo', '...']);
like image 544
Leandro Lima Avatar asked Nov 08 '22 07:11

Leandro Lima


1 Answers

Check out NavigationExtras. https://angular.io/api/router/NavigationExtras Using relativeTo and relative path, you can navigate

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

Example:

const appRoutes = [
  {
    path: 'p1', component: P1,
    children: [
      {
        path: ':id',
        component: P1C
      }
    ]
  },
  {
    path: 'p2', component: P2,
    children: [
      {
        path: ':id',
        component: P2C
      }
    ]
  },
];

this.r.navigate(['../../p1', '2'], {relativeTo: this.a})
like image 136
xdeepakv Avatar answered Nov 14 '22 21:11

xdeepakv