How to return to angular2 applications?

I am creating a single page application with several views served by a router.

Most of the information I read says to enter location:Location as a service, and then use (click)='location.back' to return to viewing.

This does not work for me, as if you start from a certain view and press "back", you end the exit from the application and go to the previous site. I also often use page reloads when using this navigation method. Is there a history API or some other way to handle navigation in an ng2 application?

+6
source share
3 answers

you can use location from angular common like:

 import {Location} from '@angular/common'; constructor(private _location: Location){ } goback(){ this._location.back(); } 
+19
source

Use this:

 window.history.back(); 
+1
source

May be useful

 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); } 
0
source

Source: https://habr.com/ru/post/1014490/


All Articles