Skip to content

Commit 73f6ed9

Browse files
jasonadenIgorMinar
authored andcommitted
feat(router): allow passing state to routerLink directives (#27198)
This value will get written to the `history.state` entry. FW-613 (related) Related to #24617 PR Close #27198
1 parent 67f4a5d commit 73f6ed9

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎packages/router/src/directives/router_link.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,27 @@ import {UrlTree} from '../url_tree';
7777
* </a>
7878
* ```
7979
*
80+
* You can provide a `state` value to be persisted to the browser's History.state
81+
* property (See https://developer.mozilla.org/en-US/docs/Web/API/History#Properties). It's
82+
* used as follows:
83+
*
84+
* ```
85+
* <a [routerLink]="['/user/bob']" [state]="{tracingId: 123}">
86+
* link to user component
87+
* </a>
88+
* ```
89+
*
90+
* And later the value can be read from the router through `router.getCurrentTransition.
91+
* For example, to capture the `tracingId` above during the `NavigationStart` event:
92+
*
93+
* ```
94+
* // Get NavigationStart events
95+
* router.events.pipe(filter(e => e instanceof NavigationStart)).subscribe(e => {
96+
* const transition = router.getCurrentTransition();
97+
* tracingService.trace({id: transition.extras.state});
98+
* });
99+
* ```
100+
*
80101
* The router link directive always treats the provided input as a delta to the current url.
81102
*
82103
* For instance, if the current url is `/user/(box//aux:team)`.
@@ -104,6 +125,7 @@ export class RouterLink {
104125
@Input() skipLocationChange !: boolean;
105126
// TODO(issue/24571): remove '!'.
106127
@Input() replaceUrl !: boolean;
128+
@Input() state?: {[k: string]: any};
107129
private commands: any[] = [];
108130
// TODO(issue/24571): remove '!'.
109131
private preserve !: boolean;
@@ -185,6 +207,7 @@ export class RouterLinkWithHref implements OnChanges, OnDestroy {
185207
@Input() skipLocationChange !: boolean;
186208
// TODO(issue/24571): remove '!'.
187209
@Input() replaceUrl !: boolean;
210+
@Input() state?: {[k: string]: any};
188211
private commands: any[] = [];
189212
private subscription: Subscription;
190213
// TODO(issue/24571): remove '!'.
@@ -237,6 +260,7 @@ export class RouterLinkWithHref implements OnChanges, OnDestroy {
237260
const extras = {
238261
skipLocationChange: attrBoolValue(this.skipLocationChange),
239262
replaceUrl: attrBoolValue(this.replaceUrl),
263+
state: this.state
240264
};
241265
this.router.navigateByUrl(this.urlTree, extras);
242266
return false;

‎packages/router/test/integration.spec.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,6 +1878,36 @@ describe('Integration', () => {
18781878

18791879
expect(location.path()).toEqual('/team/22/simple?q=1#f');
18801880
})));
1881+
1882+
it('should support history state',
1883+
fakeAsync(inject([Router, Location], (router: Router, location: SpyLocation) => {
1884+
const fixture = createRoot(router, RootCmp);
1885+
1886+
router.resetConfig([{
1887+
path: 'team/:id',
1888+
component: TeamCmp,
1889+
children: [
1890+
{path: 'link', component: LinkWithState},
1891+
{path: 'simple', component: SimpleCmp}
1892+
]
1893+
}]);
1894+
1895+
router.navigateByUrl('/team/22/link');
1896+
advance(fixture);
1897+
1898+
const native = fixture.nativeElement.querySelector('a');
1899+
expect(native.getAttribute('href')).toEqual('/team/22/simple');
1900+
native.click();
1901+
advance(fixture);
1902+
1903+
expect(fixture.nativeElement).toHaveText('team 22 [ simple, right: ]');
1904+
1905+
// Check the history entry
1906+
const history = (location as any)._history;
1907+
1908+
expect(history[history.length - 1].state.foo).toBe('bar');
1909+
expect(history[history.length - 1].state).toEqual({foo: 'bar', navigationId: history.length});
1910+
})));
18811911
});
18821912

18831913
describe('redirects', () => {
@@ -4576,6 +4606,13 @@ class RelativeLinkCmp {
45764606
class LinkWithQueryParamsAndFragment {
45774607
}
45784608

4609+
@Component({
4610+
selector: 'link-cmp',
4611+
template: `<a [routerLink]="['../simple']" [state]="{foo: 'bar'}">link</a>`
4612+
})
4613+
class LinkWithState {
4614+
}
4615+
45794616
@Component({selector: 'simple-cmp', template: `simple`})
45804617
class SimpleCmp {
45814618
}
@@ -4770,6 +4807,7 @@ class LazyComponent {
47704807
RelativeLinkCmp,
47714808
DummyLinkWithParentCmp,
47724809
LinkWithQueryParamsAndFragment,
4810+
LinkWithState,
47734811
CollectParamsCmp,
47744812
QueryParamsAndFragmentCmp,
47754813
StringLinkButtonCmp,
@@ -4797,6 +4835,7 @@ class LazyComponent {
47974835
RelativeLinkCmp,
47984836
DummyLinkWithParentCmp,
47994837
LinkWithQueryParamsAndFragment,
4838+
LinkWithState,
48004839
CollectParamsCmp,
48014840
QueryParamsAndFragmentCmp,
48024841
StringLinkButtonCmp,
@@ -4826,6 +4865,7 @@ class LazyComponent {
48264865
RelativeLinkCmp,
48274866
DummyLinkWithParentCmp,
48284867
LinkWithQueryParamsAndFragment,
4868+
LinkWithState,
48294869
CollectParamsCmp,
48304870
QueryParamsAndFragmentCmp,
48314871
StringLinkButtonCmp,

0 commit comments

Comments
 (0)