[Fixed] How to go back last page


Is there a smart way to go back last page in Angular 2?

Something like


For example, page C has a Go Back button,

  • Page A -> Page C, click it, back to page A.

  • Page B -> Page C, click it, back to page B.

Does router have this history information?


Actually you can take advantage of the built-in Location service, which owns a “Back” API.

Here (in TypeScript):

import {Component} from '@angular/core';
import {Location} from '@angular/common';

  // component's declarations here
class SomeComponent {

  constructor(private _location: Location) 

  backClicked() {

Edit: As mentioned by @charith.arumapperuma Location should be imported from @angular/common so the import {Location} from '@angular/common'; line is important.

Leave a Reply

(*) Required, Your email will not be published