Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to navigate back in angular2 apps?

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?

like image 221
userqwert Avatar asked Jan 27 '17 12:01

userqwert


2 Answers

you can use location from angular common like:

import {Location} from '@angular/common';
    constructor(private _location: Location){ }
    goback(){
        this._location.back();
    }
like image 108
Amee Prajapati Avatar answered Oct 27 '22 21:10

Amee Prajapati


Use This:

window.history.back();
like image 22
Anil Samal Avatar answered Oct 27 '22 22:10

Anil Samal