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

Advanced Routing @Angular Days 2020 Berlin/Online

Advanced Routing @Angular Days 2020 Berlin/Online

Manfred Steyer

October 07, 2020
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. Inhalt • Basics: Kurze Wiederholung • Hierarchisches Routing • Aux

    Routes • Guards • Resolver • Router Events • Lazy Loading und Preloading Page ▪ 3
  2. Über mich • Manfred Steyer SOFTWAREarchitekt.at • Angular Trainings and

    Consultancy • Google Developer Expert (GDE) • Trusted Collaborator for Angular Page ▪ 4 Manfred Steyer Remote or In-House http://www.softwarearchitekt.at/workshops
  3. Routing in Angular Page ▪ 6 Logo + Menü Menü

    2 Fußzeile SPA Passagier- Komponente /FlugDemo/passagier
  4. Konfiguration Page ▪ 12 const APP_ROUTES: Routes = [ {

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

    HttpModule, FormsModule, RouterModule.forRoot(APP_ROUTES); ], […] }) export class AppModule { } Für Root-Module Für Feature-Module: forChild
  6. Hierarchische Views Page ▪ 28 Logo + Menü Menü 2

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

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

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

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

    Flug editieren Passagier suchen Ihre Buchungen
  11. Aux-Routes Page ▪ 36 Logo + Menu Menu 2 Footer

    SPA Placeholder Named Placeholder
  12. Aux-Routes Page ▪ 37 Logo + Menu Menu 2 Footer

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

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

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

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

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

    }}]"> Activate Info </a> <a [routerLink]="[{outlets: { aux: null }}]"> Deactivate Info </a>
  18. 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. Programmatisch routen Page ▪ 46 export class AppComponent { constructor(private

    router: Router) { } activateInfo() { this.router.navigate([{outlets: { aux: 'info' }}]); } deactivateInfo() { this.router.navigate([{outlets: { aux: null }}]); } }
  20. Guards in der Konfiguration Page ▪ 52 const APP_ROUTES: Routes

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

    [ FlugEditGuard, AuthGuard ], […] }) export class AppModule { }
  22. 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
  23. Resolver @Injectable() export class FlightResolver implements Resolve<Flight> { constructor(private flightService:

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

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

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

    Verbesserung der Start-Performance Page ▪ 65
  27. Root Module mit Lazy Loading Page ▪ 66 const APP_ROUTE_CONFIG:

    Routes = [ { path: '', redirectTo: 'home', pathMatch: 'full' }, { path: 'home', component: HomeComponent }, { path: 'flights', loadChildren: '[…]flight-booking.module#FlightBookingModule' } ];
  28. Root Module mit Lazy Loading Page ▪ 67 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); } ];
  29. Routen für Feature Module Page ▪ 68 const FLUG_ROUTES =

    [ { path: '', component: FlugBuchenComponent, […] }, […] }
  30. Routen für Feature Module Page ▪ 69 const FLUG_ROUTES =

    [ { path: 'subroute', component: FlugBuchenComponent, […] }, […] }
  31. 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 ▪ 73
  32. Preloading aktivieren Page ▪ 74 export const AppRoutesModule = RouterModule.forRoot(

    ROUTE_CONFIG, { preloadingStrategy: PreloadAllModules });
  33. Kontakt and Downloads [mail] [email protected] [web] SOFTWAREarchitekt.at [twitter] ManfredSteyer d

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