Enterprise Angular Applications with NgRx and Nx
  • Introduction
  • Introduction
    • Introduction
    • Course Description
    • Resources
    • 0 - Environment Setup
    • 1a - Brief Introduction To Angular
    • 1b - Brief Introduction To Angular
    • 1c - Brief Introduction To Angular
    • 2 - Creating an Nx Workspace
    • 3 - Generating components and Nx lib
    • 4 - Add JSON server
    • 5 - Angular Services
    • 6 - Angular Material
    • 7 - Reactive Forms
    • 8 - Layout Lib and BehaviorSubjects
    • 9 - Route Guards and Products Lib
    • 10 - NgRx Introduction
    • 11 - Adding NgRx to Nx App
    • 12 - Strong Typing the State and Actions
    • 13 - NgRx Effects
    • 14 - NgRx Selectors
    • 15 - Add Products NgRx Feature Module
    • 16 - Entity State Adapter
    • 17 - Router Store
    • 18 - Deploying An Nx Monorepo
Powered by GitBook
On this page
  • 1. Refactor to use lazy loaded User module
  • 2. Move all User related folders into the user module folder
  • 3. Remove all references to Users in App module
  • 4. Change routes to lazy loaded routes
  • 5. Add UserList component as feature module route
  • 6. Declare UserList component in new User module
  • 7. Add button on App component to navigate to UserList or Home component
  • 8. Run the application and inspect the lazy loaded JavaScript
  • Extras
  • 1. Add extra project to CLI App
  • 2. Add npm package library to your app
  1. Introduction

1c - Brief Introduction To Angular

Previous1b - Brief Introduction To AngularNext2 - Creating an Nx Workspace

Last updated 6 years ago

1. Refactor to use lazy loaded User module

ng g module user --routing

2. Move all User related folders into the user module folder

3. Remove all references to Users in App module

src/app/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { HttpClientModule } from '@angular/common/http';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

4. Change routes to lazy loaded routes

src/app/app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'users', loadChildren: 'src/app/user/user.module#UserModule' },   ///changed
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

5. Add UserList component as feature module route

src/app/user/user-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { UserListComponent } from './user-list/user-list.component'; // changed

const routes: Routes = [
  { path: '', component: UserListComponent },    //changed
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class UserRoutingModule { }

6. Declare UserList component in new User module

src/app/user/user.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { UserListComponent } from './user-list/user-list.component';   //added
import { UserRoutingModule } from './user-routing.module';

@NgModule({
  imports: [
    CommonModule,
    UserRoutingModule
  ],
  declarations: [
    UserListComponent            //added
  ]
})
export class UserModule { }

7. Add button on App component to navigate to UserList or Home component

src/app/app.component.html
<h1>The App</h1>
​
<div>
  <button [routerLink]="['/']" routerLinkActive="router-link-active" >home</button>
  <button [routerLink]="['/users']" routerLinkActive="router-link-active" >users</button>
</div>

<!-- <app-home></app-home> -->
<router-outlet></router-outlet>

8. Run the application and inspect the lazy loaded JavaScript

Extras

1. Add extra project to CLI App

In the terminal run

ng generate application my-other-app

2. Add npm package library to your app

In the terminal run

ng generate library my-lib

As of Angular CLI version 6+ you can now have multiple projects in an Angular app. It is early days and no support for sharing modules between apps and best practices or schematics made. That is what Nx can help with. You can read more on the Angular CLI limited docs ​

Angular CLI v6 comes with library support via plugged into the build system we use in Angular CLI, together with schematics for generating a library.

https://github.com/angular/angular-cli/wiki/stories-multiple-projects
ng-packagr
https://github.com/angular/angular-cli/wiki/stories-create-library
User feature folder
Bundled feature module
Multi application support in default Angular CLI App
Added npm library