I am migrating an existing angular 4 app from PathLocationStrategy to HashLocationStrategy
and need to keep entry point url working. It looks something like
Since HashLocationStrategy
is already used as default, PathLocationStrategy
should be additionally injected in order to get to real browser location:
providers: [
PathLocationStrategy,
{provide: LocationStrategy, useClass: HashLocationStrategy},
...
]
...
class AppComponent {
constructor(router: Router, pathLocationStrategy: PathLocationStrategy) {
const basePath = pathLocationStrategy.getBaseHref();
const absolutePathWithParams = pathLocationStrategy.path();
if (basePath !== absolutePathWithParams) {
router.navigateByUrl(absolutePathWithParams);
}
}
}
If there is base url, it should be additionally taken away from the path.