I have built an app that uses router 3.0.0-beta.1
to switch between app sections. I also use location.go()
to emulate the switch between subsections of the same page. I used <base href="/">
and a few URL rewrite rules in order to redirect all routes to index.html
in case of page refresh. This allows the router to receive the requested subsection as a URL param. Basically I have managed to avoid using the 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 things happen:
logation.go()
updates the URL with the necessary string in order to indicate the current subsection
- A custom
scrollTo()
animation scrolls the page at the top of the requested subsection.
If I refresh the page I am using the previously defined route and extract the necessary parameter to restore scroll to the requested subsection.
this._activatedRoute.params
.map(params => params['topCategory'])
.subscribe(topCategory => {
if (typeof topCategory !== 'undefined' &&
topCategory !== null
) {
self.UiState.startArrowWasDismised = true;
self.UiState.selectedTopCategory = topCategory;
}
});
All works fine except when I click the back button. If previous page was a different section, the app router behaves as expected. However if the previous page/url was a subsection, the url changes to the previous one, but nothing happens in the UI. How can I detect if the back button was pressed in order to invoke the scrollTo()
function to do it's job again?
Most answers I saw relly on the event onhashchange
, but this event does not get fired in my app since I have no hash in the URL afterall...
question from:
https://stackoverflow.com/questions/39132737/angular-2-how-to-detect-back-button-press-using-router-and-location-go 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…