refactor(router): cleanup RouterLink

This commit is contained in:
Victor Berchet 2016-12-01 15:45:40 -08:00 committed by Alex Rickabaugh
parent dea59165de
commit 804943c9b1
2 changed files with 36 additions and 53 deletions

View File

@ -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:
*
* ```
* <a routerLink='/user/bob'>link to user component</a>
* ```
* `<a routerLink='/user/bob'>link to user component</a>`
*
* @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:
*
* ```
* <a routerLink="/user/bob">link to user component</a>
* ```
* `<a routerLink="/user/bob">link to user component</a>`
*
* 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:
*
* ```
* <a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" fragment="education">link to user
component</a>
* <a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" fragment="education">
* link to user component
* </a>
* ```
* 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:
*
* ```
* <a [routerLink]="['/user/bob']" preserveQueryParams preserveFragment>link to user
component</a>
* <a [routerLink]="['/user/bob']" preserveQueryParams preserveFragment>
* link to user component
* </a>
* ```
*
* 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;
}

View File

@ -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;