I'm building a single page application with multiple views served by a router.
Most of the info I have read says to inject location:Location
as a service, then use (click)='location.back'
to go back a view.
This doesn't work for me as if you start on a certain view and click back, you end up exiting the app and going to the previous website. I also get frequent page reloads when using this as a navigation method. Is there a history API, or some other way to handle navigation in an ng2 application?
you can use location from angular common like:
import {Location} from '@angular/common';
constructor(private _location: Location){ }
goback(){
this._location.back();
}
用这个:
window.history.back();
May be helpful
history: string[] = [];
constructor(private router: Router) {
router.events
.filter(event => event instanceof NavigationEnd)
.subscribe(event => {
this.history.push(event.url);
});
}
onBack() {
if (this.history.length > 1) {
// pop current page
this.history.pop();
// pop previous page (later it will be added)
this.history.pop();
window.history.back();
}
}
onHome() {
window.location.replace(window.location.origin);
}
On ionic 4 this worked for me:
import { Location } from '@angular/common';
...
constructor(private location: Location) {}
back() {
this.location.back();
}
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.