Angular2:如何在使用 angular2 路由时找出上一页 url 是什么 [英] Angular2: How to find out what was previous page url when using angular2 routing

查看:61
本文介绍了Angular2:如何在使用 angular2 路由时找出上一页 url 是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 angular2 应用程序并且我使用路由器.我在页面/myapp/signup 中.注册后,我将使用导航到/myapp/login./myapp/login 也可以从我的主页/myapp 导航.所以现在,当用户 users/myapp/login 我回去.这可以通过使用 location.back() 来完成.但是当用户来自/myapp/signup 时,我不想回去.所以我应该检查用户是否来自/myapp/signup ,如果是,我想将它定向到其他地方.我怎样才能知道以前的 url 以便我将用户引导到基于它的特定状态?

I am developing an angular2 app and I use router. I am in a page /myapp/signup. after signup I navigate the use to /myapp/login. /myapp/login can also be navigated from my home page which is /myapp. So now, when the user users /myapp/login I go back. This can be done by using location.back(). But I dont want to go back when the user is coming from /myapp/signup. So I should check if the user is coming from /myapp/signup and if so, I want to direct it to somewhere else. How can I know the previous url so that I direct the user to a specific state based on that ?

推荐答案

pairwise 运算符允许将当前值与前一个值一起获取

The pairwise operator allows to get the current together with the previous value

更新  

import 'rxjs/add/operator/pairwise';
import 'rxjs/add/operator/filter';
import { Router, NavigationEnd } from '@angular/router';

export class AppComponent {
    constructor(private router: Router) {
        this.router.events
        .filter(e => e instanceof NavigationEnd)
        .pairwise().subscribe((e) => {
            console.log(e);
        });
    }
}

另请参阅如何检测路线变化角度?

原创(超老)

注入Router并订阅事件并存储以备后用

Inject Router and subscribe to events and store them for later reference

constructor(private _router: Router) {
  this._router.subscribe(route => {
    this.nextRoute ...
    this.prevRoute ...
  });

这篇关于Angular2:如何在使用 angular2 路由时找出上一页 url 是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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