Search code examples
angularangular2-routingrouter-outletng-component-outlet

Angular named router outlet doesn't work as expected


I've an angular project with the following routing structure, starting from the app root:

  // app-routing.module.ts
  const routes: Routes = [
    {
      path: 'admin',
      loadChildren: () => import('./features/admin/admin.module').then((m) => m.AdminModule)
    },
  ];
  // Import and export...

  // admin-rounting.module.ts
  const routes: Routes = [
    {
      path: 'admin',
      component: ShellComponent,
      children: [
        {
          path: 'editor',
          loadChildren: () => import('./editor/editor.module').then((m) => m.EditorModule),
        },
      ],
    },
  ];
  // Import and export...

  // editor-routing.module.ts
  const routes: Routes = [
    {
      path: '',
      component: EditorComponent,
    },
    {
      path: 'view-profile',
      component: ViewProfileComponent,
      outlet: 'profile',
    },
    {
      path: 'edit-profile',
      component: EditProfileComponent,
      outlet: 'profile',
    },
  ];
  // Import and export...

The root outlet it's in the AppComponent, afterwards the ShellComponent contains another router outlet that is passed as content to a SidebarComponent that displays it in a mat-sidenav-content, something like this:

    // app.component.html
    <router-outlet><router-outlet>

    // shell.component.html
    <sidebar>
        <router-outlet outlet></router-outlet>
    </sidebar>

    // sidebar.component.html
    <mat-sidenav-content>
        <div class="content">
            <ng-content select="[outlet]"></ng-content>
        </div>
    </mat-sidenav-content>

So when I navigate to /admin/editor everything works as expected: I can see the ShellComponent and the EditorComponent is shown in the right place. The problem starts inside the EditorComponent. This component contains a <profile></profile> where I want to display the <view-profile></view-profile> or <edit-profile></edit-profile> component using the outlet named profile (as you can see in the EditorRoutingModule configuration. Here the code:

   // editor.component.html
   <profile></profile>

   // profile.component.html
   <router-outlet name="profile"></router-outlet>

   // profile.component.ts
   ngOnInit(): void {
      this.router.navigate([{ outlets: { profile: ['view-profile'] } }], { relativeTo: this.route });
   }

The ProfileComponent loads but I don't see the <view-profile></view-profile> as I expect. Inspecting the HTML I can see the profile outlet where I expect it.

Any suggestion about it?


Solution

  • I made it work but I don't know if it should be like this or if it's a workaround. Anyway, I gave a path to the root of the EditorRoutingModule and added the named routes as its children, like this:

      const routes: Routes = [
        {
          path: 'outlets',
          component: EditorComponent,
          children: [
            { path: 'view-profile', component: ViewProfileComponent, outlet: 'profile' },
            { path: 'edit-profile', component: EditProfileComponent, outlet: 'profile' },
          ],
        },
        { path: '', redirectTo: 'outlets', pathMatch: 'full' },
      ];
    

    I don't like it completely because my route looks like /admin/editor/outlets/(profile:edit-profile), is it possible to achieve the same result without the /outlets/ part in the route?