如何返回上一页 [英] How to go back last page

查看:53
本文介绍了如何返回上一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular 2中有返回上一页的明智方法吗?

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

类似

this._router.navigate(LASTPAGE);

例如,页面C具有返回按钮,

For example, page C has a Go Back button,

  • 页面A->页面C,单击它,回到页面A.

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

页面B->页面C,单击它,回到页面B.

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

路由器是否有此历史记录信息?

Does router have this history information?

推荐答案

实际上,您可以利用内置的位置服务,该服务拥有后退" API.

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

此处(在TypeScript中):

Here (in TypeScript):

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

@Component({
  // component's declarations here
})
class SomeComponent {

  constructor(private _location: Location) 
  {}

  backClicked() {
    this._location.back();
  }
}

编辑:如@ charith.a提到的那样,arumapperuma Location应该从@angular/common导入,因此import {Location} from '@angular/common';行很重要.

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

这篇关于如何返回上一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆