$30 off During Our Annual Pro Sale. View Details »

Angular's Future without NgModules: Architectures with Standalone Components

Angular's Future without NgModules: Architectures with Standalone Components

Manfred Steyer
PRO

September 27, 2022
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @ManfredSteyer Angular's Future without NgModules: Architectures with Standalone Components

  2. @ManfredSteyer

  3. @ManfredSteyer @Component({ standalone: true, selector: 'app-root', imports: [ HomeComponent, AboutComponent,

    HttpClientModule, ], templateUrl: '…' }) export class AppComponent { […] }
  4. @ManfredSteyer

  5. @ManfredSteyer Agenda #1 Mental Model #2 #3 Routing & Lazy

    Loading #4 Structuring Applications
  6. @ManfredSteyer Manfred Steyer

  7. @ManfredSteyer

  8. @ManfredSteyer Standalone Component = Component + NgModule (not implemented that

    way!)
  9. @ManfredSteyer @Component({ standalone: true, selector: 'app-root', imports: [ HomeComponent, AboutComponent,

    HttpClientModule, ], templateUrl: '…' }) export class AppComponent { […] } @NgModule({ imports: [ HttpClientModule ], declares: [ HomeComponent, AboutComponent ] }) export class AppModule { … } @Component({ selector: 'app-root', templateUrl: '…' }) export class AppComponent { […] }
  10. @ManfredSteyer @Pipe({ standalone: true, name: 'city', pure: true }) export

    class CityPipe implements PipeTransform { […] }
  11. @ManfredSteyer @Directive({ standalone: true, selector: 'input[appCity]', providers: [ … ]

    }) export class CityValidator implements Validator { […] }
  12. @ManfredSteyer @Component({ standalone: true, imports: [ […], FlightCardComponent, CityPipe, CityValidator,

    ], selector: 'flight-search', templateUrl: '…' }) export class FlightSearchComponent { […] }
  13. @ManfredSteyer

  14. @ManfredSteyer It looks like you want to use NgIfDirective and

    MyComponent. Shall I import it for you?
  15. @ManfredSteyer

  16. @ManfredSteyer

  17. @ManfredSteyer → @Component({ standalone: true, selector: 'app-root', imports: [ […]

    TicketsModule, ], templateUrl: '…' }) export class AppComponent { }
  18. @ManfredSteyer → @Component({ standalone: true, selector: 'app-root', imports: [ […]

    TicketsModule, ], templateUrl: '…' }) export class AppComponent { }
  19. @ManfredSteyer → @NgModule({ imports: [ FlightCardComponent, ], declarations: [ MyTicketsComponent

    ], }) export class TicketsModule { }
  20. @ManfredSteyer → @NgModule({ imports: [ FlightCardComponent, ], declarations: [ MyTicketsComponent

    ], }) export class TicketsModule { }
  21. @ManfredSteyer bootstrapApplication(AppComponent, { providers: [ MyGlobalService, importProvidersFrom(HttpClientModule), importProvidersFrom(RouterModule.forRoot(APP_ROUTES)), ] });

  22. @ManfredSteyer bootstrapApplication(AppComponent, { providers: [ MyGlobalService, importProvidersFrom(HttpClientModule), importProvidersFrom(RouterModule.forRoot(APP_ROUTES)), ] });

  23. @ManfredSteyer

  24. @ManfredSteyer

  25. @ManfredSteyer bootstrapApplication(AppComponent, { providers: [ […] ] });

  26. @ManfredSteyer bootstrapApplication(AppComponent, { providers: [ MyGlobalService, importProvidersFrom(HttpClientModule), importProvidersFrom(RouterModule.forRoot(APP_ROUTES)), ] });

  27. @ManfredSteyer bootstrapApplication(AppComponent, { providers: [ MyGlobalService, importProvidersFrom(HttpClientModule), provideRouter(APP_ROUTES, withPreloading(PreloadAllModules), withDebugTracing(),

    ), ] });
  28. @ManfredSteyer export const APP_ROUTES: Routes = [ […], { path:

    'flight-booking', loadChildren: () => import('@nx-example/booking/feature-book') .then(m => m.FLIGHT_BOOKING_ROUTES) }, […] ];
  29. @ManfredSteyer export const APP_ROUTES: Routes = [ […], { path:

    'flight-booking', loadChildren: () => import('@nx-example/booking/feature-book') .then(m => m.FLIGHT_BOOKING_ROUTES) }, […] ];
  30. @ManfredSteyer export const APP_ROUTES: Routes = [ […], { path:

    'flight-booking', loadChildren: () => import('@nx-example/booking/feature-book') .then(m => m.FLIGHT_BOOKING_ROUTES) }, { path: 'next-flight', loadComponent: () => import('@nx-example/booking/feature-tickets') .then(m => m.NextFlightComponent) }, ];
  31. @ManfredSteyer export const FLIGHT_BOOKING_ROUTES: Routes = [{ path: '', component:

    FlightBookingComponent, providers: [ MyService ], children: [ […] ] }];
  32. @ManfredSteyer export const FLIGHT_BOOKING_ROUTES: Routes = [{ path: '', component:

    FlightBookingComponent, providers: [ MyService ], children: [ […] ] }]; Scope: This route + all child routes (Lazily) loaded with route config If possible, use providedIn: 'root'
  33. @ManfredSteyer export const FLIGHT_BOOKING_ROUTES: Routes = [{ path: '', component:

    FlightBookingComponent, providers: [ provideState(bookingFeature), provideEffects([BookingEffects]) ], children: [ […] ] }];
  34. @ManfredSteyer

  35. @ManfredSteyer

  36. @ManfredSteyer

  37. @ManfredSteyer // index.ts == Public API export * from './flight-booking.routes';

  38. @ManfredSteyer

  39. @ManfredSteyer

  40. @ManfredSteyer + Generates path mappings + Generates initial barrel +

    Prevents bypassing index.ts + Restricting access between libraries
  41. @ManfredSteyer

  42. @ManfredSteyer

  43. @ManfredSteyer

  44. @ManfredSteyer Free eBook (5th Edition) ANGULARarchitects.io/book Module Federation & Nx

  45. @ManfredSteyer

  46. @ManfredSteyer

  47. @ManfredSteyer

  48. @ManfredSteyer

  49. @ManfredSteyer

  50. @ManfredSteyer provideRouter & withXYZ Directly point to lazy router configs

    Folders & Barrels Nx, Libs, and Constraints FTW!
  51. @ManfredSteyer d Slides & Examples Remote Company Workshops and Consulting

    http://angulararchitects.io