Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular : Relative navigation on a named router outlet

I have the following Angular Route configuration

const appRoutes: Routes = [
    {
      path: '',
      component: DirectoryComponent
    },
    {
      path: 'manage/:type/:id',
      component: ManageComponent,
      outlet: 'manage',
      children: [
        {
          path: '',
          component: PreviewComponent,
          pathMatch: 'full'
        },
        {
          path: 'add/:elementType',
          component: EditComponent,
        }
      ]
    }
  ];

ManageComponent is a sandbox component in which PreviewComponent and EditComponent will be rendered.

An user use case redirect the user to http://localhost:4200/#/(manage:manage/bar/12762) which match the preview component. Everything is okay here.

From the PreviewComponent and when the user clicks on a button, I want to make a relative navigation to the EditComponent to have, when the navigation finish, http://localhost:4200/#/(manage:manage/bar/12762/add/foo)

I tried

this.router.navigate([{outlets: {manage: ['add', 'foo']}}],{relativeTo: this.route});

and

this.router.navigate([{outlets: {manage: ['add', 'foo']}}]);

But every time, user is redirected to http://localhost:4200/#/add/foo.

How can I please make this navigation ?

like image 229
Radouane ROUFID Avatar asked Jun 13 '17 14:06

Radouane ROUFID


People also ask

What does router outlet do in Angular?

The Router-Outlet is a directive that's available from the router library where the Router inserts the component that gets matched based on the current browser's URL. You can add multiple outlets in your Angular application which enables you to implement advanced routing scenarios.

What is difference between routerLink and routerLink?

What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.

What is relative routing in Angular?

So by the use of relative path you are making your links free that are relative to the current URL segment. Your feature area of routing will be the same even if you change the route for the parent. You are just making your link free even if you change the parent route path.


Video Answer


1 Answers

I know that it's an old question, but I found a solution while I'm looking for an answer.

You can use { relativeTo: this.activatedRoute.parent } and everything works like a charm.

like image 79
Andrea Cognini Avatar answered Oct 25 '22 03:10

Andrea Cognini