Angular Standalone Tutorial - Part 2: Project Graph

Run the command: npx nx graph. A browser should open up with the following contents:

Loading...

Nx creates the graph based on the source code. The projects are not linked in this diagram because we haven't actually finished our application. Once we use the shared components in another project, Nx will create the dependency in the graph. Let's do that now.

Set Up the Router

Configure the routes:

src/app/app.module.ts
1import { NgModule } from '@angular/core'; 2import { BrowserModule } from '@angular/platform-browser'; 3import { RouterModule } from '@angular/router'; 4import { SharedUiModule } from '@store/shared/ui'; 5 6import { AppComponent } from './app.component'; 7import { NxWelcomeComponent } from './nx-welcome.component'; 8import { ShopComponent } from './shop/shop.component'; 9 10@NgModule({ 11 declarations: [AppComponent, NxWelcomeComponent, ShopComponent], 12 imports: [ 13 BrowserModule, 14 SharedUiModule, 15 RouterModule.forRoot([ 16 { 17 path: 'cart', 18 loadChildren: () => import('@store/cart').then((m) => m.CartModule), 19 }, 20 { 21 path: '**', 22 component: ShopComponent, 23 }, 24 ]), 25 ], 26 providers: [], 27 bootstrap: [AppComponent], 28}) 29export class AppModule {} 30
src/app/app.component.html
1<router-outlet></router-outlet> 2
Typescript Paths

When a library is created, Nx adds a new Typescript path to the tsconfig.base.json file. The running Typescript server process inside of your editor sometimes doesn't pick up these changes and you have to restart the server to remove inline errors on your import statements. This can be done in VS Code from the command palette when viewing a typescript file (Command-Shift-P) "Typescript: Restart TS server"

shared-ui

Run the @nx/angular:component generator with the command:

~/store

npx nx g @nx/angular:component banner --project=shared-ui --export

1 2> NX Generating @nx/angular:component 3 4CREATE shared/ui/src/lib/banner/banner.component.css 5CREATE shared/ui/src/lib/banner/banner.component.html 6CREATE shared/ui/src/lib/banner/banner.component.spec.ts 7CREATE shared/ui/src/lib/banner/banner.component.ts 8UPDATE shared/ui/src/lib/shared-ui.module.ts 9UPDATE shared/ui/src/index.ts 10
Nx 15 and lower use @nrwl/ instead of @nx/

Then create a simple Banner component in the generated file:

shared/ui/src/lib/banner/banner.component.ts
1import { Component, Input } from '@angular/core'; 2 3@Component({ 4 selector: 'store-banner', 5 templateUrl: './banner.component.html', 6 styleUrls: ['./banner.component.css'], 7}) 8export class BannerComponent { 9 @Input() text = ''; 10} 11
shared/ui/src/lib/banner/banner.component.html
1<header>{{ text }}</header> 2

cart

Create a cart-route component:

~/store

npx nx g @nx/angular:component cart-route --project=cart

1 2> NX Generating @nx/angular:component 3 4CREATE cart/src/lib/cart-route/cart-route.component.css 5CREATE cart/src/lib/cart-route/cart-route.component.html 6CREATE cart/src/lib/cart-route/cart-route.component.spec.ts 7CREATE cart/src/lib/cart-route/cart-route.component.ts 8UPDATE cart/src/lib/cart.module.ts 9
Nx 15 and lower use @nrwl/ instead of @nx/

Add the Banner component to the cart route and link back to the main page:

cart/src/lib/cart.module.ts
1import { NgModule } from '@angular/core'; 2import { CommonModule } from '@angular/common'; 3import { RouterModule } from '@angular/router'; 4import { CartRouteComponent } from './cart-route/cart-route.component'; 5import { SharedUiModule } from '@store/shared/ui'; 6 7@NgModule({ 8 declarations: [CartRouteComponent], 9 imports: [ 10 CommonModule, 11 SharedUiModule, 12 RouterModule.forChild([ 13 { 14 path: '', 15 component: CartRouteComponent, 16 }, 17 ]), 18 ], 19}) 20export class CartModule {} 21
cart/src/lib/cart-route/cart-route.component.html
1<store-banner text="Welcome to the cart." ></store-banner> 2<a routerLink="/">Continue Shopping</a> 3

store

Update the shop component to use the Banner component and link to the cart.

src/app/shop/shop.component.html
1<store-banner text="Here is a list of products to buy..." ></store-banner> 2<a routerLink="/cart">View Cart</a> 3

Now run npx nx graph again:

Loading...

Your graph now shows the dependency lines we expected.

The Project Graph is more than just a visualization - Nx provides tooling to optimize your task-running and even automate your CI based on this graph. This will be covered in more detail in: 4: Task Pipelines.

What's Next