I created an application that uses router 3.0.0-beta.1 to switch between sections of the application. I also use location.go() to emulate a switch between sub-sections of the same page. I used <base href="/"> and some URL rewriting rules to redirect all routes to index.html in case of page refresh. This allows the router to receive the requested subkey as a URL parameter. I mostly managed to avoid using HashLocationStrategy .
routes.ts
export const routes: RouterConfig = [ { path: '', redirectTo: '/catalog', pathMatch: 'full' }, { path: 'catalog', component: CatalogComponent }, { path: 'catalog/:topCategory', component: CatalogComponent }, { path: 'summary', component: SummaryComponent } ];
If I click on a subsection in the navigation bar 2, everything happens:
logation.go() updates the URL with the necessary string to indicate the current subkey- Custom animation
scrollTo() scrolls the page at the top of the requested subkey.
If I refresh the page, I use the previously defined route and extract the necessary option to restore the scroll to the required subkey.
this._activatedRoute.params .map(params => params['topCategory']) .subscribe(topCategory => { if (typeof topCategory !== 'undefined' && topCategory !== null ) { self.UiState.startArrowWasDismised = true; self.UiState.selectedTopCategory = topCategory; } });
Everything works fine, except when I click the back button. If the previous page was a different section, the application router behaves as expected. However, if the previous page / url was a subdivision, the url changes to the previous one, but nothing happens in the user interface. How to determine if the back button has been pressed to call scrollTo() to work again?
Most of the answers I've seen relate to the onhashchange event, but this event does not fire in my application since I don't have a hash in the url after ...
source share