7

I'm trying to add navigation to components in addition to the navigation of the app root, but it is not navigating to component route.

I'm using two router-outlet:

  • router outlet for the app root (app.component.html)
  • router outlet for users component (users.component.html)

With two routes:

  • route for the app (app.routing.module.ts)
  • route for users (users.routing.module.ts)

When navigating to "users" I can see users.component.html with two links, but when pressing on the link "list", it is not navigating to: UsersListComponent defined in users.routing.module.ts

I'm not sure if this is the correct way to do this.

Below is folder structure of the project and source code:

-- app.routing.module.ts
-- app.component.html
-- components
   -- dashboard
   -- users
      -- users.component.html
      -- users.module.ts
      -- users.routing.module.ts

app.routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { UsersModule } from './panels/users/users.module';

const AppRouting: Routes = [
    { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
    { path: 'dashboard', component: DashboardComponent },
    { path: 'users', loadChildren: () => UsersModule  }
    { path: '**', component: DashboardComponent }
];

@NgModule({
    imports: [RouterModule.forRoot(AppRouting, { enableTracing: true })],
    exports: [RouterModule],
    providers: []
})
export class AppRoutingModule { }

app.component.html

<div class="app-wrapper">
    <router-outlet></router-outlet>
</div>

users.component.html

<a [routerLinkActive]="['active']" [routerLink]="[{ outlets: { 'users':['list'] } }]" [skipLocationChange]="true">List</a>&nbsp;
<a [routerLinkActive]="['active']" [routerLink]="['create']" [skipLocationChange]="true">Create</a>&nbsp;
<router-outlet name="users"></router-outlet>

users.routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule, RouterOutlet, RouterLink, RouterLinkActive } from '@angular/router';
import { CreateUserComponent } from './user-create/user-create.component';
import { UsersListComponent } from './users-list/users-list.component';
import { UsersComponent } from './users.component';

const UsersRouting: Routes = [
     { path: 'list', component: UsersListComponent, outlet: 'users' },
     { path: 'create', component: CreateUserComponent, outlet: 'users' },
     { path: 'edit', component: CreateUserComponent, outlet: 'users' },
     { path: '', component: UsersComponent }
];

@NgModule({
  imports: [
      RouterModule.forChild(UsersRouting)
  ],
  declarations: [],
  exports: [
      RouterModule
  ],
})
export class UsersRoutingModule {}

users.module.ts

import { NgModule } from '@angular/core';
import { CreateUserComponent } from './user-create/user-create.component';
import { UsersComponent } from './users.component';
import { UsersListComponent } from './users-list/users-list.component';
import { UsersRoutingModule } from './users.routing.module';

@NgModule({
  imports: [
    UsersRoutingModule,
  ],
  declarations: [
    UsersComponent,
    UsersListComponent,
    CreateUserComponent
  ]
})
export class UsersModule { }

Thanks in advance

Israel
  • 445
  • 2
  • 5
  • 15

2 Answers2

12

Do you really need a named router outlet?

<router-outlet name="users"></router-outlet>

Or could you just use a child router outlet:

<router-outlet></router-outlet>

I have a video about routing here that may help: https://www.youtube.com/watch?v=LaIAHOSKHCQ&t=1s

DeborahK
  • 57,520
  • 12
  • 104
  • 129
  • 1
    Thanks for your help. 1. I removed the named router 2. In all the child routes added path: '', with children: [] Now it is working :) – Israel Aug 13 '18 at 07:46
  • 1
    Usually I'd [snarkily?] comment that if the content at a link is important to your answer, you should pull that info in, but this is a great exception. That video was a perfect add-on resource. I need to rewatch after the leopard slide, but **I'll recommend to everyone here to go watch the video** if you're new-ish to Angular routing and/or `router-outlet`'s use. – ruffin Sep 11 '19 at 20:49
  • The video is a fantastic source of information on the main and children router-outlet. Any reference to how the first option you mentioned "router-outlet name="users">" works? – Srikanth Chakravarthy Jul 11 '23 at 01:15
1

Yo... just specify the outlet to be a custom outlet https://angular.io/guide/router#add-a-secondary-route

so you could say

{
  path: 'compose',
  component: ComposeMessageComponent,
  outlet: 'popup'
},

Then this component will render in a <popup></popup> component

Joey Gough
  • 2,753
  • 2
  • 21
  • 42