Revert "feat(router): add hash-based navigation option to setUpLocationSync (#28609)"
This reverts commit fc895ba1893d08813f931c8794c7db97782f1c36. This is change is a feature and doesn't belong on the patch branch.
This commit is contained in:
parent
cfe4732e41
commit
843fc7df9f
@ -58,7 +58,7 @@ export function locationSyncBootstrapListener(ngUpgrade: UpgradeModule) {
|
|||||||
*
|
*
|
||||||
* @publicApi
|
* @publicApi
|
||||||
*/
|
*/
|
||||||
export function setUpLocationSync(ngUpgrade: UpgradeModule, urlType: 'path' | 'hash' = 'path') {
|
export function setUpLocationSync(ngUpgrade: UpgradeModule) {
|
||||||
if (!ngUpgrade.$injector) {
|
if (!ngUpgrade.$injector) {
|
||||||
throw new Error(`
|
throw new Error(`
|
||||||
RouterUpgradeInitializer can be used only after UpgradeModule.bootstrap has been called.
|
RouterUpgradeInitializer can be used only after UpgradeModule.bootstrap has been called.
|
||||||
@ -71,16 +71,7 @@ export function setUpLocationSync(ngUpgrade: UpgradeModule, urlType: 'path' | 'h
|
|||||||
|
|
||||||
ngUpgrade.$injector.get('$rootScope')
|
ngUpgrade.$injector.get('$rootScope')
|
||||||
.$on('$locationChangeStart', (_: any, next: string, __: string) => {
|
.$on('$locationChangeStart', (_: any, next: string, __: string) => {
|
||||||
let url;
|
const url = resolveUrl(next);
|
||||||
if (urlType === 'path') {
|
|
||||||
url = resolveUrl(next);
|
|
||||||
} else if (urlType === 'hash') {
|
|
||||||
// Remove the first hash from the URL
|
|
||||||
const hashIdx = next.indexOf('#');
|
|
||||||
url = resolveUrl(next.substring(0, hashIdx) + next.substring(hashIdx + 1));
|
|
||||||
} else {
|
|
||||||
throw 'Invalid URLType passed to setUpLocationSync: ' + urlType;
|
|
||||||
}
|
|
||||||
const path = location.normalize(url.pathname);
|
const path = location.normalize(url.pathname);
|
||||||
router.navigateByUrl(path + url.search + url.hash);
|
router.navigateByUrl(path + url.search + url.hash);
|
||||||
});
|
});
|
||||||
|
@ -79,30 +79,6 @@ describe('setUpLocationSync', () => {
|
|||||||
expect(RouterMock.navigateByUrl).toHaveBeenCalledWith(normalizedPathname + query + hash);
|
expect(RouterMock.navigateByUrl).toHaveBeenCalledWith(normalizedPathname + query + hash);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should allow configuration to work with hash-based routing', () => {
|
|
||||||
const url = 'https://google.com';
|
|
||||||
const pathname = '/custom/route';
|
|
||||||
const normalizedPathname = 'foo';
|
|
||||||
const query = '?query=1&query2=3';
|
|
||||||
const hash = '#new/hash';
|
|
||||||
const combinedUrl = url + '#' + pathname + query + hash;
|
|
||||||
const $rootScope = jasmine.createSpyObj('$rootScope', ['$on']);
|
|
||||||
|
|
||||||
upgradeModule.$injector.get.and.returnValue($rootScope);
|
|
||||||
LocationMock.normalize.and.returnValue(normalizedPathname);
|
|
||||||
|
|
||||||
setUpLocationSync(upgradeModule, 'hash');
|
|
||||||
|
|
||||||
const callback = $rootScope.$on.calls.argsFor(0)[1];
|
|
||||||
callback({}, combinedUrl, '');
|
|
||||||
|
|
||||||
expect(LocationMock.normalize).toHaveBeenCalledTimes(1);
|
|
||||||
expect(LocationMock.normalize).toHaveBeenCalledWith(pathname);
|
|
||||||
|
|
||||||
expect(RouterMock.navigateByUrl).toHaveBeenCalledTimes(1);
|
|
||||||
expect(RouterMock.navigateByUrl).toHaveBeenCalledWith(normalizedPathname + query + hash);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should work correctly on browsers that do not start pathname with `/`', () => {
|
it('should work correctly on browsers that do not start pathname with `/`', () => {
|
||||||
const anchorProto = HTMLAnchorElement.prototype;
|
const anchorProto = HTMLAnchorElement.prototype;
|
||||||
const originalDescriptor = Object.getOwnPropertyDescriptor(anchorProto, 'pathname');
|
const originalDescriptor = Object.getOwnPropertyDescriptor(anchorProto, 'pathname');
|
||||||
@ -122,5 +98,4 @@ describe('setUpLocationSync', () => {
|
|||||||
Object.defineProperty(anchorProto, 'pathname', originalDescriptor !);
|
Object.defineProperty(anchorProto, 'pathname', originalDescriptor !);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
2
tools/public_api_guard/router/upgrade.d.ts
vendored
2
tools/public_api_guard/router/upgrade.d.ts
vendored
@ -5,4 +5,4 @@ export declare const RouterUpgradeInitializer: {
|
|||||||
deps: (typeof UpgradeModule)[];
|
deps: (typeof UpgradeModule)[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export declare function setUpLocationSync(ngUpgrade: UpgradeModule, urlType?: 'path' | 'hash'): void;
|
export declare function setUpLocationSync(ngUpgrade: UpgradeModule): void;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user