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

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

问题描述

我正在开发angular2应用,并且使用路由器. 我在页面/myapp/signup中.注册后,我将使用导航到/myapp/login./myapp/login也可以从我的主页/myapp导航.所以现在,当用户用户/myapp/login时,我返回.这可以通过使用location.back()来完成.但是当用户来自/myapp/signup时,我不想返回.因此,我应该检查用户是否来自/myapp/signup,如果是,我要将其定向到其他地方. 如何知道以前的网址,以便根据该网址将用户定向到特定状态?

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天全站免登陆