refactor(router): cleanup RouterLink
This commit is contained in:
parent
dea59165de
commit
804943c9b1
|
@ -14,33 +14,23 @@ import {NavigationEnd, Router} from '../router';
|
||||||
import {ActivatedRoute} from '../router_state';
|
import {ActivatedRoute} from '../router_state';
|
||||||
import {UrlTree} from '../url_tree';
|
import {UrlTree} from '../url_tree';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @whatItDoes Lets you link to specific parts of your app.
|
* @whatItDoes Lets you link to specific parts of your app.
|
||||||
*
|
*
|
||||||
* @howToUse
|
* @howToUse
|
||||||
*
|
*
|
||||||
* Consider the following route configuration:
|
* 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:
|
* 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
|
* @description
|
||||||
*
|
*
|
||||||
* The RouterLink directives let you link to specific parts of your app.
|
* The RouterLink directives let you link to specific parts of your app.
|
||||||
*
|
*
|
||||||
* Whe the link is static, you can use the directive as follows:
|
* 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
|
* If you use dynamic values to generate the link, you can pass an array of path
|
||||||
* segments, followed by the params for each segment.
|
* 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}]`
|
* For instance `['/team', teamId, 'user', userName, {details: true}]`
|
||||||
* means that we want to generate a link to `/team/11/user/bob;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:
|
* Multiple static segments can be merged into one
|
||||||
true}]`).
|
* (e.g., `['/team/11/user', userName, {details: true}]`).
|
||||||
*
|
*
|
||||||
* The first segment name can be prepended with `/`, `./`, or `../`:
|
* 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
|
* * 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
|
* * 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.
|
* * And if the first segment begins with `../`, the router will go up one level.
|
||||||
*
|
*
|
||||||
* You can set query params and fragment as follows:
|
* You can set query params and fragment as follows:
|
||||||
*
|
*
|
||||||
* ```
|
* ```
|
||||||
* <a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" fragment="education">link to user
|
* <a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" fragment="education">
|
||||||
component</a>
|
* link to user component
|
||||||
|
* </a>
|
||||||
* ```
|
* ```
|
||||||
* RouterLink will use these to generate this link: `/user/bob#education?debug=true`.
|
* 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:
|
* You can also tell the directive to preserve the current query params and fragment:
|
||||||
*
|
*
|
||||||
* ```
|
* ```
|
||||||
* <a [routerLink]="['/user/bob']" preserveQueryParams preserveFragment>link to user
|
* <a [routerLink]="['/user/bob']" preserveQueryParams preserveFragment>
|
||||||
component</a>
|
* link to user component
|
||||||
|
* </a>
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* The router link directive always treats the provided input as a delta to the current url.
|
* 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;
|
@Input() replaceUrl: boolean;
|
||||||
private commands: any[] = [];
|
private commands: any[] = [];
|
||||||
|
|
||||||
constructor(
|
constructor(private router: Router, private route: ActivatedRoute) {}
|
||||||
private router: Router, private route: ActivatedRoute,
|
|
||||||
private locationStrategy: LocationStrategy) {}
|
|
||||||
|
|
||||||
@Input()
|
@Input()
|
||||||
set routerLink(data: any[]|string) {
|
set routerLink(data: any[]|string) {
|
||||||
|
@ -144,7 +134,6 @@ export class RouterLinkWithHref implements OnChanges, OnDestroy {
|
||||||
@Input() target: string;
|
@Input() target: string;
|
||||||
@Input() queryParams: {[k: string]: any};
|
@Input() queryParams: {[k: string]: any};
|
||||||
@Input() fragment: string;
|
@Input() fragment: string;
|
||||||
@Input() routerLinkOptions: {preserveQueryParams: boolean, preserveFragment: boolean};
|
|
||||||
@Input() preserveQueryParams: boolean;
|
@Input() preserveQueryParams: boolean;
|
||||||
@Input() preserveFragment: boolean;
|
@Input() preserveFragment: boolean;
|
||||||
@Input() skipLocationChange: boolean;
|
@Input() skipLocationChange: boolean;
|
||||||
|
@ -208,7 +197,6 @@ export class RouterLinkWithHref implements OnChanges, OnDestroy {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function toBool(s?: any): boolean {
|
function toBool(s: any): boolean {
|
||||||
if (s === '') return true;
|
return s === '' || !!s;
|
||||||
return !!s;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -700,27 +700,25 @@ describe('Integration', () => {
|
||||||
})));
|
})));
|
||||||
|
|
||||||
it('should not deactivate aux routes when navigating from a componentless routes',
|
it('should not deactivate aux routes when navigating from a componentless routes',
|
||||||
fakeAsync(inject(
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||||
[Router, Location, NgModuleFactoryLoader],
|
const fixture = createRoot(router, TwoOutletsCmp);
|
||||||
(router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => {
|
|
||||||
const fixture = createRoot(router, TwoOutletsCmp);
|
|
||||||
|
|
||||||
router.resetConfig([
|
router.resetConfig([
|
||||||
{path: 'simple', component: SimpleCmp},
|
{path: 'simple', component: SimpleCmp},
|
||||||
{path: 'componentless', children: [{path: 'simple', component: SimpleCmp}]},
|
{path: 'componentless', children: [{path: 'simple', component: SimpleCmp}]},
|
||||||
{path: 'user/:name', outlet: 'aux', component: UserCmp}
|
{path: 'user/:name', outlet: 'aux', component: UserCmp}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
router.navigateByUrl('/componentless/simple(aux:user/victor)');
|
router.navigateByUrl('/componentless/simple(aux:user/victor)');
|
||||||
advance(fixture);
|
advance(fixture);
|
||||||
expect(location.path()).toEqual('/componentless/simple(aux:user/victor)');
|
expect(location.path()).toEqual('/componentless/simple(aux:user/victor)');
|
||||||
expect(fixture.nativeElement).toHaveText('[ simple, aux: user victor ]');
|
expect(fixture.nativeElement).toHaveText('[ simple, aux: user victor ]');
|
||||||
|
|
||||||
router.navigateByUrl('/simple(aux:user/victor)');
|
router.navigateByUrl('/simple(aux:user/victor)');
|
||||||
advance(fixture);
|
advance(fixture);
|
||||||
expect(location.path()).toEqual('/simple(aux:user/victor)');
|
expect(location.path()).toEqual('/simple(aux:user/victor)');
|
||||||
expect(fixture.nativeElement).toHaveText('[ simple, aux: user victor ]');
|
expect(fixture.nativeElement).toHaveText('[ simple, aux: user victor ]');
|
||||||
})));
|
})));
|
||||||
|
|
||||||
it('should emit an event when an outlet gets activated', fakeAsync(() => {
|
it('should emit an event when an outlet gets activated', fakeAsync(() => {
|
||||||
@Component({
|
@Component({
|
||||||
|
@ -766,7 +764,7 @@ describe('Integration', () => {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it('should update url and router state before activating components',
|
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);
|
const fixture = createRoot(router, RootCmp);
|
||||||
|
|
||||||
|
@ -843,8 +841,7 @@ describe('Integration', () => {
|
||||||
]);
|
]);
|
||||||
})));
|
})));
|
||||||
|
|
||||||
it('should handle errors',
|
it('should handle errors', fakeAsync(inject([Router], (router: Router) => {
|
||||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
||||||
const fixture = createRoot(router, RootCmp);
|
const fixture = createRoot(router, RootCmp);
|
||||||
|
|
||||||
router.resetConfig(
|
router.resetConfig(
|
||||||
|
@ -865,8 +862,7 @@ describe('Integration', () => {
|
||||||
expect(e).toEqual('error');
|
expect(e).toEqual('error');
|
||||||
})));
|
})));
|
||||||
|
|
||||||
it('should preserve resolved data',
|
it('should preserve resolved data', fakeAsync(inject([Router], (router: Router) => {
|
||||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
||||||
const fixture = createRoot(router, RootCmp);
|
const fixture = createRoot(router, RootCmp);
|
||||||
|
|
||||||
router.resetConfig([{
|
router.resetConfig([{
|
||||||
|
@ -890,7 +886,7 @@ describe('Integration', () => {
|
||||||
})));
|
})));
|
||||||
|
|
||||||
it('should rerun resolvers when the urls segments of a wildcard route change',
|
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);
|
const fixture = createRoot(router, RootCmp);
|
||||||
|
|
||||||
router.resetConfig([{
|
router.resetConfig([{
|
||||||
|
@ -899,7 +895,6 @@ describe('Integration', () => {
|
||||||
resolve: {numberOfUrlSegments: 'numberOfUrlSegments'}
|
resolve: {numberOfUrlSegments: 'numberOfUrlSegments'}
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
const e: any = null;
|
|
||||||
router.navigateByUrl('/one/two');
|
router.navigateByUrl('/one/two');
|
||||||
advance(fixture);
|
advance(fixture);
|
||||||
const cmp = fixture.debugElement.children[1].componentInstance;
|
const cmp = fixture.debugElement.children[1].componentInstance;
|
||||||
|
|
Loading…
Reference in New Issue