# ng-japan OnAir vol.18 Show Note [https://hackmd.io/@lacolaco/BJf0yV828](https://hackmd.io/@lacolaco/BJf0yV828) https://connpass.com/event/178718 --- ## Router API再発見 - RouterModule - Route - RouterOutlet - Router - RouterLink - ActivatedRoute https://stackblitz.com/angular/egnnljjvrgj?file=src%2Fapp%2Fapp.component.html ## RouterModule https://angular.jp/api/router/RouterModule ```typescript class RouterModule { static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders<RouterModule> static forChild(routes: Routes): ModuleWithProviders<RouterModule> } interface ExtraOptions { enableTracing?: boolean useHash?: boolean initialNavigation?: InitialNavigation errorHandler?: ErrorHandler preloadingStrategy?: any onSameUrlNavigation?: 'reload' | 'ignore' scrollPositionRestoration?: 'disabled' | 'enabled' | 'top' anchorScrolling?: 'disabled' | 'enabled' scrollOffset?: [number, number] | (() => [number, number]) paramsInheritanceStrategy?: 'emptyOnly' | 'always' malformedUriErrorHandler?: (error: URIError, urlSerializer: UrlSerializer, url: string) => UrlTree urlUpdateStrategy?: 'deferred' | 'eager' relativeLinkResolution?: 'legacy' | 'corrected' } ``` ## Route https://angular.jp/api/router/Route - Route / Routes ```typescript interface Route { path?: string pathMatch?: string matcher?: UrlMatcher component?: Type<any> redirectTo?: string outlet?: string canActivate?: any[] canActivateChild?: any[] canDeactivate?: any[] canLoad?: any[] data?: Data resolve?: ResolveData children?: Routes loadChildren?: LoadChildren runGuardsAndResolvers?: RunGuardsAndResolvers } ``` ```typescript interface CanActivate { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree } interface CanActivateChild { canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree } interface CanDeactivate<T> { canDeactivate(component: T, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot, nextState?: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree } ``` ## RouterOutlet ```htmlmixed <router-outlet (activate)='onActivate($event)' (deactivate)='onDeactivate($event)'></router-outlet> ``` ## Router https://angular.jp/api/router/Router ```typescript= class Router { events: Observable<Event> routerState: RouterState errorHandler: ErrorHandler malformedUriErrorHandler: (error: URIError, urlSerializer: UrlSerializer, url: string) => UrlTree navigated: boolean urlHandlingStrategy: UrlHandlingStrategy routeReuseStrategy: RouteReuseStrategy onSameUrlNavigation: 'reload' | 'ignore' paramsInheritanceStrategy: 'emptyOnly' | 'always' urlUpdateStrategy: 'deferred' | 'eager' relativeLinkResolution: 'legacy' | 'corrected' config: Routes url: string initialNavigation(): void setUpLocationChangeListener(): void getCurrentNavigation(): Navigation | null resetConfig(config: Routes): void ngOnDestroy(): void dispose(): void createUrlTree(commands: any[], navigationExtras: NavigationExtras = {}): UrlTree navigateByUrl(url: string | UrlTree, extras: NavigationExtras = { skipLocationChange: false }): Promise<boolean> navigate(commands: any[], extras: NavigationExtras = { skipLocationChange: false }): Promise<boolean> serializeUrl(url: UrlTree): string parseUrl(url: string): UrlTree isActive(url: string | UrlTree, exact: boolean): boolean } ``` ### UrlTree ```typescript interface UrlTree { root: UrlSegmentGroup queryParams: Params fragment: string | null queryParamMap: ParamMap toString(): string } const tree: UrlTree = router.parseUrl('/team/33/(user/victor//support:help)?debug=true#fragment'); const f = tree.fragment; // return 'fragment' const q = tree.queryParams; // returns {debug: 'true'} const g: UrlSegmentGroup = tree.root.children[PRIMARY_OUTLET]; const s: UrlSegment[] = g.segments; // returns 2 segments 'team' and '33' g.children[PRIMARY_OUTLET].segments; // returns 2 segments 'user' and 'victor' g.children['support'].segments; // return 1 segment 'help' ``` ### NavigationExtras ```typescript= interface NavigationExtras { relativeTo?: ActivatedRoute | null queryParams?: Params | null fragment?: string queryParamsHandling?: QueryParamsHandling | null preserveFragment?: boolean skipLocationChange?: boolean replaceUrl?: boolean state?: {...} } ``` ## RouterLink ```htmlmixed <a routerLink="/" [queryParams]="{ key: 'value' }" [fragment]="'foo'" [queryParamsHandling]="'merge'" [preserveFragment]="true" [skipLocationChange]="true" [replaceUrl]="true" [state]="{ key: 'value' }" ></a> ``` ###### tags: `ng_jp_onair`