博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何返回上一页
阅读量:2289 次
发布时间:2019-05-09

本文共 1835 字,大约阅读时间需要 6 分钟。

本文翻译自:

Is there a smart way to go back last page in Angular 2? 有没有一种聪明的方法可以返回Angular 2的最后一页?

Something like 就像是

this._router.navigate(LASTPAGE);

For example, page C has a Go Back button, 例如,页面C具有“返回”按钮,

  • Page A -> Page C, click it, back to page A. 页面A->页面C,单击它,回到页面A。

  • Page B -> Page C, click it, back to page B. 页面B->页面C,单击它,返回页面B。

Does router have this history information? 路由器是否有此历史记录信息?


#1楼

参考:


#2楼

You can implement routerOnActivate() method on your route class, it will provide information about previous route. 您可以在路由类上实现routerOnActivate()方法,它将提供有关先前路由的信息。

routerOnActivate(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction) : any

Then you can use router.navigateByUrl() and pass data generated from ComponentInstruction . 然后,您可以使用router.navigateByUrl()并传递从ComponentInstruction生成的数据。 For example: 例如:

this._router.navigateByUrl(prevInstruction.urlPath);

#3楼

Actually you can take advantage of the built-in Location service, which owns a "Back" API. 实际上,您可以利用内置的位置服务,该服务拥有“后退” API。

Here (in TypeScript): 此处(在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();  }}

#4楼

自Beta 18开始:

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


#5楼

在RC4中:

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

#6楼

In the final version of Angular 2.x / 4.x - here's the docs 在Angular 2.x / 4.x的最终版本中-这是文档

/* 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  }}

转载地址:http://iwjnb.baihongyu.com/

你可能感兴趣的文章
poj1511——Invitation Cards(SPFA+邻接表)
查看>>
poj3159——Candies(差分约束+SPFA堆栈)
查看>>
Lightoj1074——Extended Traffic(SPFA判断负环)
查看>>
poj1062——昂贵的聘礼(dijkstra变形)
查看>>
poj1847——Tram(dijkstra)
查看>>
hdu4725——The Shortest Path in Nya Graph(SPFA+两层图)
查看>>
山东省第四届ACM省赛题——Proxy(dijistra+多条最短路)
查看>>
poj3169——Layout(差分约束+SPFA判断负环)
查看>>
poj1611——The Suspects(并查集)
查看>>
hdu1272——小希的迷宫(并查集+判断环)
查看>>
poj1308——Is It A Tree?(判断是否为树)
查看>>
poj1251——Jungle Roads(prim)
查看>>
poj1287——Networking(prim)
查看>>
poj2031——Building a Space Station(prim)
查看>>
poj2412——Constructing Roads(最小生成树变形)
查看>>
poj1789——Truck History(最小生成树+prim)
查看>>
poj2349——Arctic Network(最小生成树+prim)
查看>>
poj1751——Highways(部分确定的最小生成树)
查看>>
hdu1875——畅通工程再续(最小生成树)
查看>>
Lightoj1189——Sum of Factorials(阶乘的和+贪心)
查看>>