From 804943c9b1af9b31184fd9e601a3ca96892456a7 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Thu, 1 Dec 2016 15:45:40 -0800 Subject: [PATCH] refactor(router): cleanup RouterLink --- .../router/src/directives/router_link.ts | 44 +++++++----------- .../@angular/router/test/integration.spec.ts | 45 +++++++++---------- 2 files changed, 36 insertions(+), 53 deletions(-) diff --git a/modules/@angular/router/src/directives/router_link.ts b/modules/@angular/router/src/directives/router_link.ts index d7eaf8291b..e8bca982a1 100644 --- a/modules/@angular/router/src/directives/router_link.ts +++ b/modules/@angular/router/src/directives/router_link.ts @@ -14,33 +14,23 @@ import {NavigationEnd, Router} from '../router'; import {ActivatedRoute} from '../router_state'; import {UrlTree} from '../url_tree'; - /** * @whatItDoes Lets you link to specific parts of your app. * * @howToUse * * Consider the following route configuration: - - * ``` - * [{ path: 'user/:name', component: UserCmp }] - * ``` + * `[{ path: 'user/:name', component: UserCmp }]` * * When linking to this `user/:name` route, you can write: - * - * ``` - * link to user component - * ``` + * `link to user component` * * @description * * The RouterLink directives let you link to specific parts of your app. * * Whe the link is static, you can use the directive as follows: - * - * ``` - * link to user component - * ``` + * `link to user component` * * If you use dynamic values to generate the link, you can pass an array of path * segments, followed by the params for each segment. @@ -48,29 +38,31 @@ import {UrlTree} from '../url_tree'; * For instance `['/team', teamId, 'user', userName, {details: true}]` * means that we want to generate a link to `/team/11/user/bob;details=true`. * - * Multiple static segments can be merged into one (e.g., `['/team/11/user', userName, {details: - true}]`). + * Multiple static segments can be merged into one + * (e.g., `['/team/11/user', userName, {details: true}]`). * * The first segment name can be prepended with `/`, `./`, or `../`: * * If the first segment begins with `/`, the router will look up the route from the root of the - app. + * app. * * If the first segment begins with `./`, or doesn't begin with a slash, the router will - * instead look in the children of the current activated route. + * instead look in the children of the current activated route. * * And if the first segment begins with `../`, the router will go up one level. * * You can set query params and fragment as follows: * * ``` - * link to user - component + * + * link to user component + * * ``` * RouterLink will use these to generate this link: `/user/bob#education?debug=true`. * * You can also tell the directive to preserve the current query params and fragment: * * ``` - * link to user - component + * + * link to user component + * * ``` * * The router link directive always treats the provided input as a delta to the current url. @@ -97,9 +89,7 @@ export class RouterLink { @Input() replaceUrl: boolean; private commands: any[] = []; - constructor( - private router: Router, private route: ActivatedRoute, - private locationStrategy: LocationStrategy) {} + constructor(private router: Router, private route: ActivatedRoute) {} @Input() set routerLink(data: any[]|string) { @@ -144,7 +134,6 @@ export class RouterLinkWithHref implements OnChanges, OnDestroy { @Input() target: string; @Input() queryParams: {[k: string]: any}; @Input() fragment: string; - @Input() routerLinkOptions: {preserveQueryParams: boolean, preserveFragment: boolean}; @Input() preserveQueryParams: boolean; @Input() preserveFragment: boolean; @Input() skipLocationChange: boolean; @@ -208,7 +197,6 @@ export class RouterLinkWithHref implements OnChanges, OnDestroy { } } -function toBool(s?: any): boolean { - if (s === '') return true; - return !!s; +function toBool(s: any): boolean { + return s === '' || !!s; } diff --git a/modules/@angular/router/test/integration.spec.ts b/modules/@angular/router/test/integration.spec.ts index 0ffd0709a9..d4013eb6e5 100644 --- a/modules/@angular/router/test/integration.spec.ts +++ b/modules/@angular/router/test/integration.spec.ts @@ -700,27 +700,25 @@ describe('Integration', () => { }))); it('should not deactivate aux routes when navigating from a componentless routes', - fakeAsync(inject( - [Router, Location, NgModuleFactoryLoader], - (router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => { - const fixture = createRoot(router, TwoOutletsCmp); + fakeAsync(inject([Router, Location], (router: Router, location: Location) => { + const fixture = createRoot(router, TwoOutletsCmp); - router.resetConfig([ - {path: 'simple', component: SimpleCmp}, - {path: 'componentless', children: [{path: 'simple', component: SimpleCmp}]}, - {path: 'user/:name', outlet: 'aux', component: UserCmp} - ]); + router.resetConfig([ + {path: 'simple', component: SimpleCmp}, + {path: 'componentless', children: [{path: 'simple', component: SimpleCmp}]}, + {path: 'user/:name', outlet: 'aux', component: UserCmp} + ]); - router.navigateByUrl('/componentless/simple(aux:user/victor)'); - advance(fixture); - expect(location.path()).toEqual('/componentless/simple(aux:user/victor)'); - expect(fixture.nativeElement).toHaveText('[ simple, aux: user victor ]'); + router.navigateByUrl('/componentless/simple(aux:user/victor)'); + advance(fixture); + expect(location.path()).toEqual('/componentless/simple(aux:user/victor)'); + expect(fixture.nativeElement).toHaveText('[ simple, aux: user victor ]'); - router.navigateByUrl('/simple(aux:user/victor)'); - advance(fixture); - expect(location.path()).toEqual('/simple(aux:user/victor)'); - expect(fixture.nativeElement).toHaveText('[ simple, aux: user victor ]'); - }))); + router.navigateByUrl('/simple(aux:user/victor)'); + advance(fixture); + expect(location.path()).toEqual('/simple(aux:user/victor)'); + expect(fixture.nativeElement).toHaveText('[ simple, aux: user victor ]'); + }))); it('should emit an event when an outlet gets activated', fakeAsync(() => { @Component({ @@ -766,7 +764,7 @@ describe('Integration', () => { })); it('should update url and router state before activating components', - fakeAsync(inject([Router, Location], (router: Router, location: Location) => { + fakeAsync(inject([Router], (router: Router) => { const fixture = createRoot(router, RootCmp); @@ -843,8 +841,7 @@ describe('Integration', () => { ]); }))); - it('should handle errors', - fakeAsync(inject([Router, Location], (router: Router, location: Location) => { + it('should handle errors', fakeAsync(inject([Router], (router: Router) => { const fixture = createRoot(router, RootCmp); router.resetConfig( @@ -865,8 +862,7 @@ describe('Integration', () => { expect(e).toEqual('error'); }))); - it('should preserve resolved data', - fakeAsync(inject([Router, Location], (router: Router, location: Location) => { + it('should preserve resolved data', fakeAsync(inject([Router], (router: Router) => { const fixture = createRoot(router, RootCmp); router.resetConfig([{ @@ -890,7 +886,7 @@ describe('Integration', () => { }))); it('should rerun resolvers when the urls segments of a wildcard route change', - fakeAsync(inject([Router, Location], (router: Router, location: Location) => { + fakeAsync(inject([Router, Location], (router: Router) => { const fixture = createRoot(router, RootCmp); router.resetConfig([{ @@ -899,7 +895,6 @@ describe('Integration', () => { resolve: {numberOfUrlSegments: 'numberOfUrlSegments'} }]); - const e: any = null; router.navigateByUrl('/one/two'); advance(fixture); const cmp = fixture.debugElement.children[1].componentInstance;