Skip to content

Commit 1c00ab4

Browse files
endlacerkirjs
authored andcommitted
feat(router): extend paramters of RedirectFunction to include paramMap and queryParamMap
adds paramMap and queryParamMap to the partial ActivatedRoute of the RedirectFn fixes #angular#60842
1 parent 85ce5f3 commit 1c00ab4

4 files changed

Lines changed: 55 additions & 4 deletions

File tree

goldens/public-api/router/index.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ export class RedirectCommand {
612612
}
613613

614614
// @public
615-
export type RedirectFunction = (redirectData: Pick<ActivatedRouteSnapshot, 'routeConfig' | 'url' | 'params' | 'queryParams' | 'fragment' | 'data' | 'outlet' | 'title'>) => MaybeAsync<string | UrlTree>;
615+
export type RedirectFunction = (redirectData: Pick<ActivatedRouteSnapshot, 'routeConfig' | 'url' | 'params' | 'queryParams' | 'fragment' | 'data' | 'outlet' | 'title' | 'paramMap' | 'queryParamMap'>) => MaybeAsync<string | UrlTree>;
616616

617617
// @public
618618
export interface Resolve<T> {

packages/router/src/apply_redirects.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,33 @@ function getRedirectResult(
201201
return Promise.resolve(redirectTo);
202202
}
203203
const redirectToFn = redirectTo;
204-
const {queryParams, fragment, routeConfig, url, outlet, params, data, title} = currentSnapshot;
204+
const {
205+
queryParams,
206+
fragment,
207+
routeConfig,
208+
url,
209+
outlet,
210+
params,
211+
data,
212+
title,
213+
paramMap,
214+
queryParamMap,
215+
} = currentSnapshot;
205216
return firstValueFrom(
206217
wrapIntoObservable(
207218
runInInjectionContext(injector, () =>
208-
redirectToFn({params, data, queryParams, fragment, routeConfig, url, outlet, title}),
219+
redirectToFn({
220+
params,
221+
data,
222+
queryParams,
223+
fragment,
224+
routeConfig,
225+
url,
226+
outlet,
227+
title,
228+
paramMap,
229+
queryParamMap,
230+
}),
209231
),
210232
),
211233
);

packages/router/src/models.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,16 @@ export type QueryParamsHandling = 'merge' | 'preserve' | 'replace' | '';
315315
export type RedirectFunction = (
316316
redirectData: Pick<
317317
ActivatedRouteSnapshot,
318-
'routeConfig' | 'url' | 'params' | 'queryParams' | 'fragment' | 'data' | 'outlet' | 'title'
318+
| 'routeConfig'
319+
| 'url'
320+
| 'params'
321+
| 'queryParams'
322+
| 'fragment'
323+
| 'data'
324+
| 'outlet'
325+
| 'title'
326+
| 'paramMap'
327+
| 'queryParamMap'
319328
>,
320329
) => MaybeAsync<string | UrlTree>;
321330

packages/router/test/apply_redirects.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,26 @@ describe('redirects', () => {
16551655
);
16561656
});
16571657

1658+
it('can access queryParamMap and paramMap and redirect using them', async () => {
1659+
await checkRedirect(
1660+
[
1661+
{
1662+
path: 'a/b',
1663+
redirectTo: ({queryParamMap, paramMap}) => {
1664+
const tree = TestBed.inject(Router).parseUrl(`other;id=${paramMap.get('id')}`);
1665+
tree.queryParams = {hl: queryParamMap.get('hl')};
1666+
return Promise.resolve(tree);
1667+
},
1668+
},
1669+
{path: '**', component: ComponentC},
1670+
],
1671+
'/a/b;id=123?hl=en&q=hello',
1672+
(t: UrlTree) => {
1673+
expectTreeToBe(t, 'other;id=123?hl=en');
1674+
},
1675+
);
1676+
});
1677+
16581678
it('with a function using inject and returning a UrlTree with params', async () => {
16591679
await checkRedirect(
16601680
[

0 commit comments

Comments
 (0)