Navigating between child routes of Angular 2

I cannot move between child routes.

Router Overview:

path: 'parent', component: parentComponent, children: [ { path: '', redirectTo: 'child1', pathMatch: 'full' }, { path: 'child1', component: child1Component }, { path: 'child2', component: child2Component }, { path: 'new/:type', component: child3Component }, { path: '**', component: PageNotFoundComponent } ] 

i is in the component child1, which is trying to jump to a new route / type:

 this._router.navigate(['new', objType], { relativeTo: this._route }); 

But I get an error all the time: it cannot match any routes. Segment URL: "new / asset".

If I manually paste the url, it works fine.

Help will be greatly appreciated, thanks!

+6
source share
3 answers

You can use ../

 this._router.navigate(['../new', objType], { relativeTo: this._route }); 
+11
source
 this.router.navigate(['../../new'], { relativeTo: this._route }) 

Or

 this.router.navigate(['/parent/new']) 

Or with an anchor tag:

  <a [routerLink]=" ['../../new'] "> Go to new </a> 

Or:

  <a [routerLink]=" ['/parent/new'] "> Go to new </a> 
+4
source

When you use relativeTo and then try to jump, you have to put a relative path, not just the whole one from the point of view of the parents. In your example, this should be more like:

 this._router.navigate([ '../', objType ], { relativeTo: this._route }); 
+3
source

Source: https://habr.com/ru/post/1013637/


All Articles