如何在 angular2 中获取以前的路线,所以经过一些处理使导航回到以前的 url

How to get the previous route in angular2, so after some processing making the navigation back to previous url

我有一个带有心愿单选项的页面,现在如果用户未登录并单击心愿单按钮,则会将用户重定向到登录页面,但在用户成功登录后,我想将用户带到用户单击的先前路由状态心愿单按钮 ??

我尝试使用像 RoutesRecognized 这样的路由器事件,但是如果我第二次访问登录页面时它不会第一次被触发。有什么建议请帮忙。

我什至使用了路由器事件的 pairwise() 方法,但这也是第二次触发,而不是第一次,也就是说,如果我返回第 1 页并重新导航到第 2 页,此方法会触发。从第 1 页 -> 第 2 页

首次访问时,它根本没有被激发

你可以使用angulars RouterStateSnapshot https://angular.io/api/router/RouterStateSnapshot.

import { RouterStateSnapshot } from '@angular/router';

constructor(state: RouterStateSnapshot) { }

this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } 

要在 Angular 中实施最可靠的返回上一视图的方法,您必须:

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

  • 在组件构造函数上注入位置:

    constructor( private _location: Location ) {}

  • 在所需函数上调用 back() 方法:

    public goBack() { this._location.back(); }