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

angular5

 angular5

Danila Marchenkov

September 07, 2017
Tweet

More Decks by Danila Marchenkov

Other Decks in Education

Transcript

  1. How to transition • Direct Url change • links on

    the page • back & forward browser events
  2. const appRoutes: Routes = [ { path: 'crisis-center', component: CrisisListComponent

    }, { path: 'hero/:id', component: HeroDetailComponent }, { path: 'heroes', component: HeroListComponent, data: { title: 'Heroes List' } }, { path: '', redirectTo: '/heroes', pathMatch: 'full' }, { path: '**', component: PageNotFoundComponent } ];
  3. url An Observable of the route path(s), represented as an

    array of strings for each part of the route path. data An Observable that contains the data object provided for the route. Also contains any resolved values from the resolve guard. paramMap An Observable that contains a map of the required and optional parameters specific to the route. The map supports retrieving single and multiple values from the same parameter. queryParamMap An Observable that contains a map of the query parameters available to all routes. The map supports retrieving single and multiple values from the query parameter. fragment An Observable of the URL fragment available to all routes. outlet The name of the RouterOutlet used to render the route. For an unnamed outlet, the outlet name is primary. routeConfig The route configuration used for the route that contains the origin path. parent The route's parent ActivatedRoute when this route is a child route. firstChild Contains the first ActivatedRoute in the list of this route's child routes. children Contains all the child routes activated under the current route.
  4. NavigationStart An event triggered when navigation starts. RoutesRecognized An event

    triggered when the Router parses the URL and the routes are recognized. RouteConfigLoadStart An event triggered before the Router lazy loads a route configuration. RouteConfigLoadEnd An event triggered after a route has been lazy loaded. NavigationEnd An event triggered when navigation ends successfully. NavigationCancel An event triggered when navigation is canceled. This is due to a Route Guard returning NavigationError An event triggered when navigation fails due to an unexpected error. Navigation hooks
  5. @Injectable() export class CrisisDetailResolver implements Resolve<Crisis> { constructor(private cs: CrisisService,

    private router: Router) {} resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Crisis> { let id = route.paramMap.get('id'); return this.cs.getCrisis(id).take(1).map(crisis => { if (crisis) { return crisis; } else { // id not found this.router.navigate(['/crisis-center']); return null; } }); } } import { CrisisDetailResolver } from './crisis-detail-resolver.service'; @NgModule({ imports: [ RouterModule.forChild(crisisCenterRoutes) ], exports: [ RouterModule ], providers: [ CrisisDetailResolver ] }) export class CrisisCenterRoutingModule { }
  6. @NgModule({ imports: [ RouterModule.forChild(adminRoutes) ], exports: [ RouterModule ] })

    export class AdminRoutingModule {} const adminRoutes: Routes = [ { path: 'admin', component: AdminComponent, canActivate: [AuthGuard], children: [ { path: '', canActivateChild: [AuthGuard], children: [ { path: 'crises', component: ManageCrisesComponent }, { path: 'heroes', component: ManageHeroesComponent }, { path: '', component: AdminDashboardComponent } ] } ] } ];
  7. import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild } from '@angular/router';

    import { AuthService } from './auth.service'; @Injectable() export class AuthGuard implements CanActivate, CanActivateChild { constructor(private authService: AuthService, private router: Router) {} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { let url: string = state.url; return this.checkLogin(url); } canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { return this.canActivate(route, state); } /* . . . */ }
  8. @Injectable() export class CanDeactivateGuard implements CanDeactivate<CrisisDetailComponent> { canDeactivate( component: CrisisDetailComponent,

    route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable<boolean> | boolean { if (!component.crisis || component.crisis.name === component.editName) { return true; } return component.dialogService.confirm('Discard changes?'); } } { path: ':id', component: CrisisDetailComponent, canDeactivate: [CanDeactivateGuard] }
  9. -Custom? -Sure import { Injectable } from '@angular/core'; import {

    Injectable } from '@angular/core'; import { PreloadingStrategy, Route } from '@angular/router'; import { Observable } from 'rxjs/Observable'; @Injectable() export class SelectivePreloadingStrategy implements PreloadingStrategy { preloadedModules: string[] = []; preload(route: Route, load: () => Observable<any>): Observable<any> { if (route.data && route.data['preload']) { // add the route path to the preloaded module array this.preloadedModules.push(route.path); // log the route path to the console console.log('Preloaded: ' + route.path); return load(); } else { return Observable.of(null); } } }
  10. import { RouteReuseStrategy, ActivatedRouteSnapshot, DetachedRouteHandle } from '@angular/router'; export class

    CustomReuseStrategy implements RouteReuseStrategy { handlers: {[key: string]: DetachedRouteHandle} = {}; shouldDetach(route: ActivatedRouteSnapshot): boolean { return !!route.data && !!(route.data as any).shouldReuse; } store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void { this.handlers[route.routeConfig.path] = handle; } shouldAttach(route: ActivatedRouteSnapshot): boolean { return !!route.routeConfig && !!this.handlers[route.routeConfig.path]; } retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle { if (!route.routeConfig) { return null; } return this.handlers[route.routeConfig.path]; } shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean { return future.routeConfig === curr.routeConfig; } }