Skip to content

Commit a752971

Browse files
sarunintAndrewKushnir
authored andcommitted
fix(router): add relativeLinkResolution to recognize operator (angular#26990)
Close angular#26983 PR Close angular#26990
1 parent f0c4f31 commit a752971

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

packages/router/src/operators/recognize.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import {UrlTree} from '../url_tree';
1717

1818
export function recognize(
1919
rootComponentType: Type<any>| null, config: Route[], serializer: (url: UrlTree) => string,
20-
paramsInheritanceStrategy: 'emptyOnly' |
21-
'always'): MonoTypeOperatorFunction<NavigationTransition> {
20+
paramsInheritanceStrategy: 'emptyOnly' | 'always', relativeLinkResolution: 'legacy' |
21+
'corrected'): MonoTypeOperatorFunction<NavigationTransition> {
2222
return function(source: Observable<NavigationTransition>) {
2323
return source.pipe(mergeMap(
2424
t => recognizeFn(
2525
rootComponentType, config, t.urlAfterRedirects, serializer(t.urlAfterRedirects),
26-
paramsInheritanceStrategy)
26+
paramsInheritanceStrategy, relativeLinkResolution)
2727
.pipe(map(targetSnapshot => ({...t, targetSnapshot})))));
2828
};
29-
}
29+
}

packages/router/src/router.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ export class Router {
414414
// Recognize
415415
recognize(
416416
this.rootComponentType, this.config, (url) => this.serializeUrl(url),
417-
this.paramsInheritanceStrategy),
417+
this.paramsInheritanceStrategy, this.relativeLinkResolution),
418418

419419
// Fire RoutesRecognized
420420
tap(t => {

packages/router/test/integration.spec.ts

+86
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,41 @@ describe('Integration', () => {
7676
]);
7777
})));
7878

79+
describe('relativeLinkResolution', () => {
80+
beforeEach(inject([Router], (router: Router) => {
81+
router.resetConfig([{
82+
path: 'foo',
83+
children: [{path: 'bar', children: [{path: '', component: RelativeLinkCmp}]}]
84+
}]);
85+
}));
86+
87+
it('should not ignore empty paths in legacy mode',
88+
fakeAsync(inject([Router], (router: Router) => {
89+
router.relativeLinkResolution = 'legacy';
90+
91+
const fixture = createRoot(router, RootCmp);
92+
93+
router.navigateByUrl('/foo/bar');
94+
advance(fixture);
95+
96+
const link = fixture.nativeElement.querySelector('a');
97+
expect(link.getAttribute('href')).toEqual('/foo/bar/simple');
98+
})));
99+
100+
it('should ignore empty paths in corrected mode',
101+
fakeAsync(inject([Router], (router: Router) => {
102+
router.relativeLinkResolution = 'corrected';
103+
104+
const fixture = createRoot(router, RootCmp);
105+
106+
router.navigateByUrl('/foo/bar');
107+
advance(fixture);
108+
109+
const link = fixture.nativeElement.querySelector('a');
110+
expect(link.getAttribute('href')).toEqual('/foo/simple');
111+
})));
112+
});
113+
79114
it('should set the restoredState to null when executing imperative navigations',
80115
fakeAsync(inject([Router], (router: Router) => {
81116
router.resetConfig([
@@ -4030,6 +4065,57 @@ describe('Integration', () => {
40304065
]);
40314066
})));
40324067
});
4068+
4069+
describe('relativeLinkResolution', () => {
4070+
@Component({selector: 'link-cmp', template: `<a [routerLink]="['../simple']">link</a>`})
4071+
class RelativeLinkCmp {
4072+
}
4073+
4074+
@NgModule({
4075+
declarations: [RelativeLinkCmp],
4076+
imports: [RouterModule.forChild([
4077+
{path: 'foo/bar', children: [{path: '', component: RelativeLinkCmp}]},
4078+
])]
4079+
})
4080+
class LazyLoadedModule {
4081+
}
4082+
4083+
it('should not ignore empty path when in legacy mode',
4084+
fakeAsync(inject(
4085+
[Router, NgModuleFactoryLoader],
4086+
(router: Router, loader: SpyNgModuleFactoryLoader) => {
4087+
router.relativeLinkResolution = 'legacy';
4088+
loader.stubbedModules = {expected: LazyLoadedModule};
4089+
4090+
const fixture = createRoot(router, RootCmp);
4091+
4092+
router.resetConfig([{path: 'lazy', loadChildren: 'expected'}]);
4093+
4094+
router.navigateByUrl('/lazy/foo/bar');
4095+
advance(fixture);
4096+
4097+
const link = fixture.nativeElement.querySelector('a');
4098+
expect(link.getAttribute('href')).toEqual('/lazy/foo/bar/simple');
4099+
})));
4100+
4101+
it('should ignore empty path when in corrected mode',
4102+
fakeAsync(inject(
4103+
[Router, NgModuleFactoryLoader],
4104+
(router: Router, loader: SpyNgModuleFactoryLoader) => {
4105+
router.relativeLinkResolution = 'corrected';
4106+
loader.stubbedModules = {expected: LazyLoadedModule};
4107+
4108+
const fixture = createRoot(router, RootCmp);
4109+
4110+
router.resetConfig([{path: 'lazy', loadChildren: 'expected'}]);
4111+
4112+
router.navigateByUrl('/lazy/foo/bar');
4113+
advance(fixture);
4114+
4115+
const link = fixture.nativeElement.querySelector('a');
4116+
expect(link.getAttribute('href')).toEqual('/lazy/foo/simple');
4117+
})));
4118+
});
40334119
});
40344120

40354121
describe('Custom Route Reuse Strategy', () => {

0 commit comments

Comments
 (0)