Skip to content

Commit

Permalink
fix: use native router method to initialize it
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain Marcadal committed Aug 30, 2022
1 parent da488c0 commit b4db510
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
2 changes: 0 additions & 2 deletions libs/mf-tools/src/lib/web-components/bootstrap-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,9 @@ function shareShellZone(injector: Injector) {

function connectMicroFrontendRouter(injector: Injector) {
const router = injector.get(Router);

if (!router) {
console.warn('No router to connect found');
return;
}

connectRouter(router);
}
25 changes: 11 additions & 14 deletions libs/mf-tools/src/lib/web-components/router-utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Location } from '@angular/common';
import { Router, UrlMatcher, UrlSegment } from '@angular/router';

export function startsWith(prefix: string): UrlMatcher {
Expand All @@ -20,19 +21,15 @@ export function endsWith(prefix: string): UrlMatcher {
};
}

export function connectRouter(router: Router, useHash = false): void {
let url: string;
if (!useHash) {
url = `${location.pathname.substr(1)}${location.search}`;
router.navigateByUrl(url);
window.addEventListener('popstate', () => {
router.navigateByUrl(url);
});
} else {
url = `${location.hash.substr(1)}${location.search}`;
router.navigateByUrl(url);
window.addEventListener('hashchange', () => {
router.navigateByUrl(url);
});

export function connectRouter(router: Router, newLocation?: Location) {
if (!router) {
// eslint-disable-next-line no-console
console.warn('No router to connect found');
return;
}
router.initialNavigation();
if (location) {
void router.navigateByUrl(newLocation.path(true))
}
}

0 comments on commit b4db510

Please sign in to comment.