问题描述
在 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 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.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.
这篇关于如何返回最后一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!