Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Advanced Routing @AngularDays Spring 2022

Advanced Routing @AngularDays Spring 2022

Manfred Steyer

March 23, 2022
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @ManfredSteyer Inhalt • Basics: Kurze Wiederholung • Hierarchisches Routing •

    Aux Routes • Guards • Resolver • Router Events • Lazy Loading und Preloading Page ▪ 3
  2. @ManfredSteyer Routing in Angular Page ▪ 7 Logo + Menü

    Menü 2 Fußzeile SPA Passagier- Komponente /FlugDemo/passagier
  3. @ManfredSteyer Konfiguration Page ▪ 13 const APP_ROUTES: Routes = [

    { path: 'home', component: HomeComponent }, { path: 'flug-suchen', component: FlugSuchenComponent } ]
  4. @ManfredSteyer Konfiguration Page ▪ 16 // app.module.ts @NgModule({ imports: [

    BrowserModule, HttpModule, FormsModule, RouterModule.forRoot(APP_ROUTES); ], […] }) export class AppModule { } Für Root-Module Für Feature-Module: forChild
  5. @ManfredSteyer View von AppComponent Page ▪ 19 <a routerLink="/home">Home</a> <a

    routerLink="/flug-suchen">Flug suchen</a> <div> <router-outlet></router-outlet> </div>
  6. @ManfredSteyer Hierarchische Views Page ▪ 29 Logo + Menü Menü

    2 Fußzeile SPA /FlugDemo/flugbuchen FlugBuchen-Komponente
  7. @ManfredSteyer Hierarchische Views Page ▪ 30 Logo + Menü Menü

    2 Fußzeile SPA Optionen Platzhalter FlugBuchen-Komponente /FlugDemo/flugbuchen
  8. @ManfredSteyer Hierarchische Views Page ▪ 31 Logo + Menü Menü

    2 Fußzeile SPA Optionen Passagier- Komponente FlugBuchen-Komponente /FlugDemo/flugbuchen/passagier
  9. @ManfredSteyer Konfiguration Page ▪ 32 const APP_ROUTES: Routes = [

    { path: 'flug-buchen', component: FlugBuchenComponent, children: [ { path: 'flug-suchen', component: FlugSuchenComponent }, […] ] } ];
  10. @ManfredSteyer DEMO Page ▪ 33 App Home Flug buchen Flug

    suchen Flug editieren Passagier suchen Ihre Buchungen
  11. @ManfredSteyer Aux-Routes Page ▪ 37 Logo + Menu Menu 2

    Footer SPA Placeholder Named Placeholder
  12. @ManfredSteyer Aux-Routes Page ▪ 38 Logo + Menu Menu 2

    Footer SPA Flight- Component Named Placeholder /FlightApp/flights
  13. @ManfredSteyer Aux-Routes Page ▪ 39 Logo + Menu Menu 2

    Footer SPA Flight- Component Info-Component /FlightApp/flights(aux:info)
  14. @ManfredSteyer Aux-Routes Page ▪ 40 Logo + Menu Menu 2

    Footer SPA Flight- Component Modal-Component /FlightApp/flights(aux:info/modal)
  15. @ManfredSteyer Aux-Routes Page ▪ 41 Logo + Menu Menu 2

    Footer SPA Flight-Edit- Component Modal-Component /FlightApp/flights(aux:info/modal)/17
  16. @ManfredSteyer Konfiguration Page ▪ 44 export const ROUTE_CONFIG: Routes =

    [ { path: 'home', component: HomeComponent }, { path: 'info', component: InfoComponent, outlet: 'aux' }, { path: 'dashboard', component: DashboardComponent, outlet: 'aux' } ]
  17. @ManfredSteyer Aux-Routes routen Page ▪ 45 <a [routerLink]="[{outlets: { aux:

    'info' }}]"> Activate Info </a> <a [routerLink]="[{outlets: { aux: null }}]"> Deactivate Info </a>
  18. @ManfredSteyer Mehrere Outlets gleichzeitig bestücken <a [routerLink]="[{outlets: { aux: 'basket',

    primary: 'flight-booking/flight-search' }}]"> … </a> <a [routerLink]="[{outlets: { aux: 'basket', primary: ['flight-booking', 'flight-search'] }}]"> … </a> <a [routerLink]="[{outlets: { aux: 'basket', primary: ['flight-booking', 'flight-edit', 17] }}]"> … </a>
  19. @ManfredSteyer Programmatisch routen Page ▪ 47 export class AppComponent {

    constructor(private router: Router) { } activateInfo() { this.router.navigate([{outlets: { aux: 'info' }}]); } deactivateInfo() { this.router.navigate([{outlets: { aux: null }}]); } }
  20. @ManfredSteyer Was sind Guards? • Services • Können Aktivierung und

    Deaktivierung von Routen verhindern Page ▪ 50
  21. @ManfredSteyer Guards Rückgabewert: boolean | Observable<boolean> | Promise<boolean> CanActivate canActivate

    CanActivateChild canActivateChild CanLoad canLoad CanDeactivate<T> canDeactivate
  22. @ManfredSteyer Guards in der Konfiguration Page ▪ 53 const APP_ROUTES:

    Routes = [ { path: '/flug-buchen', component: FlugBuchenComponent, canActivate: [AuthGuard], children: [ { path: 'flug-edit/:id', component: FlugEditComponent, canDeactivate: [FlugEditGuard] }, […] ] ]
  23. @ManfredSteyer Provider für Guards Page ▪ 56 // app.module.ts @NgModule({

    providers: [ FlugEditGuard, AuthGuard ], […] }) export class AppModule { }
  24. @ManfredSteyer Was sind Resolver? • Services • Werden beim Routenwechsel

    aktiv • Verzögern Aktivierung der neuen Route • Laden benötigte Daten • Zwischenzeitlich kann z. B. Loading Indikator angezeigt werden
  25. @ManfredSteyer Resolver @Injectable() export class FlightResolver implements Resolve<Flight> { constructor(private

    flightService: FlightService) { } resolve(route, state): Observable<Flight> | Promise<Flight> | Flight { return […] } }
  26. @ManfredSteyer Resolver registrieren const FLIGHT_BOOKING_ROUTES: Routes = [ […] {

    path: 'flight-edit/:id', component: FlightEditComponent, resolve: { flight: FlightResolver } } ];
  27. @ManfredSteyer Daten entgegennehmen @Component({ … }) export class FlightEditComponent {

    flight: Flight; constructor(private route: ActivatedRoute) { } ngOnInit() { this.route.data.subscribe( data => { this.flight = data['flight']; } ); } }
  28. @ManfredSteyer Warum Lazy Loading? • Module erst bei Bedarf nachladen

    • Verbesserung der Start-Performance Page ▪ 66
  29. @ManfredSteyer Root Module mit Lazy Loading Page ▪ 67 const

    APP_ROUTE_CONFIG: Routes = [ { path: '', redirectTo: 'home', pathMatch: 'full' }, { path: 'home', component: HomeComponent }, { path: 'flights', loadChildren: '[…]flight-booking.module#FlightBookingModule' } ];
  30. @ManfredSteyer Root Module mit Lazy Loading Page ▪ 68 const

    APP_ROUTE_CONFIG: Routes = [ { path: '', redirectTo: 'home', pathMatch: 'full' }, { path: 'home', component: HomeComponent }, { path: 'flights', loadChildren: () => import('[…]flight-booking.module') .then(m => m.FlightBookingModule); } ];
  31. @ManfredSteyer Routen für Feature Module Page ▪ 69 const FLUG_ROUTES

    = [ { path: '', component: FlugBuchenComponent, […] }, […] }
  32. @ManfredSteyer Routen für Feature Module Page ▪ 70 const FLUG_ROUTES

    = [ { path: 'subroute', component: FlugBuchenComponent, […] }, […] }
  33. @ManfredSteyer Idee • Eventuell später benötigte Module werden mit freien

    Ressourcen vorgeladen • Wird das Modul später tatsächlich benötigt, steht es augenblicklich zur Verfügung Page ▪ 74
  34. @ManfredSteyer Preloading aktivieren Page ▪ 75 export const AppRoutesModule =

    RouterModule.forRoot( ROUTE_CONFIG, { preloadingStrategy: PreloadAllModules });
  35. @ManfredSteyer Lazy Loading and Shared Modules Page ▪ 89 AppModule

    FlightBooking Module SharedModule includes includes (lazy) includes AuthService
  36. @ManfredSteyer Lazy Loading and Shared Modules Page ▪ 90 AppModule

    FlightBooking Module SharedModule includes includes (lazy) includes AuthService AuthService
  37. @ManfredSteyer Lazy Loading and Shared Modules Page ▪ 91 AppModule

    FlightBooking Module SharedModule includes includes (lazy) includes AuthService AuthService
  38. @ManfredSteyer Solution 1: CoreModule Page ▪ 92 AppModule FlightBooking Module

    SharedModule includes (lazy) includes CoreModule includes includes Core-Module is only imported into the AppModule Global Providers + Shell
  39. @ManfredSteyer Solution 2: forRoot Page ▪ 94 AppModule FlightBooking Module

    AuthModule includes (lazy) includes (with Services) CoreModule includes includes (without Services)
  40. @ManfredSteyer AuthModule Page ▪ 96 @NgModule({ […], providers: [] })

    export class AuthModule { static forRoot(): ModuleWithProviders<AuthModule> { return { ngModule: AuthModule, providers: [AuthService, […]] } } }
  41. @ManfredSteyer Lazy Modules Page ▪ 100 @Injectable({ providedIn: LazyApiModule })

    export class FlightService { […] } Only makes sense with lazy loading !! All "classic" modules: root scope Service is loaded alongside lazy module!
  42. @ManfredSteyer Kontakt and Downloads [mail] [email protected] [web] SOFTWAREarchitekt.at [twitter] ManfredSteyer

    d Slides & Examples Public and In-House http://www.softwarearchitekt.at/workshops