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

Routing into the Sunset with Angular

Routing into the Sunset with Angular

Talk from International JavaScript Conference in Munich, October 2017

Manfred Steyer

October 24, 2017
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. About me… • Manfred Steyer • SOFTWAREarchitekt.at • Trainer &

    Consultant • Focus: Angular • Google Developer Expert (GDE) Page ▪ 2 Manfred Steyer
  2. Routing in Angular Page ▪ 6 Logo + Menu Menu

    2 Footer SPA Passenger- Component /FlightApp/passenger
  3. Configuration Page ▪ 7 const APP_ROUTES: Routes = [ {

    path: 'home', component: HomeComponent }, { path: 'flight-search', component: FlightSearchComponent }, { path: '**', redirectTo: 'home' } ]
  4. Configuration Page ▪ 8 // app.module.ts @NgModule({ imports: [ BrowserModule,

    HttpModule, FormsModule, RouterModule.forRoot(ROUTE_CONFIG) ], […] }) export class AppModule { } For Root-Module For Feature-Module: forChild
  5. Parameters Page ▪ 11 const APP_ROUTES: Routes = [ […]

    { path: 'flight-search', component: FlightSearchComponent }, { path: 'flight-edit/:id', component: FlightEditComponent } }
  6. Reading Parameters Page ▪ 12 export class FlightEditComponent { public

    id: string; constructor( private route: ActivatedRoute) { route.params.subscribe( p => { this.id = p['id']; […] } ); } […] }
  7. Hierarchical Routing Page ▪ 16 Logo + Menu Menu 2

    Footer SPA /FlightDemo/flight-booking FlightBookingComponent
  8. Hierarchical Routing Page ▪ 17 Logo + Menu Menu 2

    Footer SPA Options Placeholder FlightBookingComponent /FlightDemo/flight-booking
  9. Hierarchical Routing Page ▪ 18 Logo + Menu Menu 2

    Footer SPA Optionen Passenger Component FlightBookingComponent /FlightDemo/flight-booking/passenger
  10. Configuration Page ▪ 19 const APP_ROUTES: Routes = [ {

    path: '', component: HomeComponent }, { path: 'flight-booking', component: FlightBookingComponent, children: [ { path: 'flight-search', component: FlightSearchComponent }, […] ] } ];
  11. DEMO Page ▪ 20 App Home Flight Booking Flight Search

    Flight Edit Passenger Search Other Stuff
  12. Aux-Routes Page ▪ 22 Logo + Menu Menu 2 Footer

    SPA Placeholder Named Placeholder
  13. Aux-Routes Page ▪ 23 Logo + Menu Menu 2 Footer

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

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

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

    SPA Flight-Edit- Component Modal-Component /FlightApp/flights(aux:info/modal)/edit/17
  17. Use Cases • Partly autonomous parts of an application •

    „Norton Commander Style“ • (CSS-based) Popups and Modals
  18. What are Guard? • Services • Can prevent the Activation

    or Deactivation of a Route Page ▪ 30
  19. Guards in der Konfiguration Page ▪ 33 const APP_ROUTES: Routes

    = [ { path: '/flight-booking', component: FlightBookingComponent, canActivate: [AuthGuard], children: [ { path: 'flight-edit/:id', component: FlightEditComponent, canDeactivate: [FlightEditGuard] }, […] ] ] Token
  20. Provider für Guards Page ▪ 35 // app.module.ts @NgModule({ providers:

    [ FlightEditGuard, AuthGuard ], […] }) export class AppModule { }
  21. Root Module with Lazy Loading Page ▪ 45 const APP_ROUTE_CONFIG:

    Routes = [ { path: 'home', component: HomeComponent }, { path: 'flights', loadChildren: './[…]flight-booking.module#FlightBookingModule' } ];
  22. Routes for Feature Module Page ▪ 46 const FLIGHT_ROUTES =

    [ { path: '/bookings', component: FlightBookingComponent, […] }, […] } Url: /flights/bookings Triggers Lazy Loading
  23. Idea • Modules that might be needed later are loaded

    after (!) the start of the application • When the module is actually needed, it is available immediately Page ▪ 49
  24. Lazy Loading and Shared Modules Page ▪ 53 AppModule FlightModule

    SharedModule includes includes (lazy) includes AuthService
  25. Lazy Loading and Shared Modules Page ▪ 54 AppModule FlightModule

    SharedModule includes includes (lazy) includes AuthService AuthService
  26. Lazy Loading and Shared Modules Page ▪ 55 AppModule FlightModule

    SharedModule includes includes (lazy) includes AuthService AuthService
  27. Solution Page ▪ 56 AppModule FlightModule SharedModule includes (lazy) includes

    CoreModule includes includes Only import CoreModule into AppModule! Global Providers like AuthService & Shell
  28. Solution Page ▪ 60 AppModule FlightModule AuthModule includes (lazy) includes

    (with Services) CoreModule includes includes (without Services)
  29. Auth Module Page ▪ 62 @NgModule({ […], providers: [] })

    export class AuthModule { static forRoot(): ModuleWithProviders { return { ngModule: AuthModule, providers: [AuthService, […]] } } }