在Angular 2中有没有聪明的方法返回到最后一页?

类似的

this._router.navigate(LASTPAGE);

例如,C页有一个“返回”按钮,

A页-> C页,点击返回A页。 B页-> C页,点击它,回到B页。

路由器有这个历史信息吗?


当前回答

另一个解决方案

window.history.back ();

其他回答

试试routerLink = " - LASTPAGE”

在Angular 2的最终版本中。X / 4。x -这里是文档https://angular.io/api/common/Location

/* typescript */

import { Location } from '@angular/common';
// import stuff here

@Component({
// declare component here
})
export class MyComponent {

  // inject location into component constructor
  constructor(private location: Location) { }

  cancel() {
    this.location.back(); // <-- go back to previous location on cancel
  }
}

如果你使用的是最新的Angular/TypeScript,请确保你显式导入

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

and

 onCancel() {
    this.location.back();
  }

RC4:

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

从beta 18开始:

import {Location} from 'angular /platform/common';