# make sure you are in the angular project folder
cd ngx-safe
# Generate new shared module
ng generate module safe
ng generate module layout
# Generate home, user and admin modules
ng g module views/home --routing
ng g module views/user --routing
ng g module views/admin --routing
# Generate home view
ng g c views/home/homeLandingPage --changeDetection OnPush --module views/home
# Generate admin view
ng g c views/admin/pages/adminLandingPage --changeDetection OnPush --module views/admin
# Generate user view
ng g c views/user/userLandingPage --changeDetection OnPush --module views/user
Replace code in app/app.component.html
<router-outlet></router-outlet>
Configure routing of the whole app in app/app-routing.module.ts
const routes: Routes = [
{
path: "admin",
loadChildren: () =>
import("./views/admin/admin.module").then(m => m.AdminModule)
},
{
path: "user",
loadChildren: () =>
import("./views/user/user.module").then(m => m.UserModule)
},
{
path: "home",
loadChildren: () =>
import("./views/home/home.module").then(m => m.HomeModule)
},
{
path: "",
redirectTo: "home",
pathMatch: "full"
}
];
Add routing configuration to app/views/home/home-routing.module.ts
const routes: Routes = [
{
path: "",
component: HomeLandingPageComponent
}
];
Add routing configuration to app/views/admin-routing.module.ts
const routes: Routes = [
{
path: "",
component: AdminLandingPageComponent
}
];
Add routing configuration to app/views/user/user-routing.module.ts
const routes: Routes = [
{
path: "",
component: UserLandingPageComponent
}
];
# Add Angular Material
ng add @angular/material
# Generate header with sidenav
ng g @angular/material:nav --name layout/header-with-sidenav --changeDetection OnPush --export --module layout --selector cool-header-with-sidenav
Replace mat-nav-list html tag with
<ng-content select="[navlist]"></ng-content>
Replace "Add Content Here" comment
<ng-content select="[body]"></ng-content>
Add content to src\app\views\home\home-landing-page\home-landing-page.component.html
<cool-header-with-sidenav>
<ng-container navlist>
<mat-nav-list>
<a mat-list-item routerLink="/admin" routerLinkActive="active"
>Login as Admin</a
>
<a mat-list-item routerLink="/user" routerLinkActive="active"
>Login as User</a
>
</mat-nav-list>
</ng-container>
<p body>home works</p>
</cool-header-with-sidenav>
Add LayoutModule and MatListModule to home.module.ts
Now you can navigate to the user and the admin page.
Hint: When loading user page, you can see that only the user module is loaded in the Chrome networks tab.
imports: [CommonModule, LayoutModule, NoopAnimationsModule, MatToolbarModule, MatButtonModule, MatSidenavModule, MatIconModule, MatListModule],
Very easy to fix.
ng g component components/PageNotFound --changeDetection OnPush
{ path: '**', component: PageNotFoundComponent }
ng g c views/user/containers/userHome --changeDetection OnPush --module views/user
ng g c views/shared/containers/safe --export --changeDetection OnPush --module shared
To see the user and admin page included into the header and navigation you need secondary routes:
Replace codein app/views/user/user.component.html
<cool-header-with-sidenav>
<ng-container navlist>
<mat-nav-list>
<a mat-list-item routerLink="" routerLinkActive="active">Home</a>
<a mat-list-item routerLink="/user" routerLinkActive="active">UserHome</a>
</mat-nav-list>
</ng-container>
<div body>
<router-outlet name="secondary"></router-outlet>
<a [routerLink]="[{outlets: { secondary: ['safe'] } }]">Safe</a>
</div>
</cool-header-with-sidenav>
Add SharedModule to user.module.ts
Add router outlet to app/app.component.html
<router-outlet></router-outlet>
Add routes to app/views/user/user.routing.module.ts
const routes: Routes = [
{
path: "home",
component: UserComponent,
children: [
{
path: "safe",
component: SafeComponent,
outlet: "secondary"
},
{
path: "",
component: UserHomeComponent,
outlet: "secondary"
}
]
},
{
path: "",
redirectTo: "home"
}
];