如何以角度 5 获取上一页 URL [英] How to get the previous page URL in angular 5

查看:36
本文介绍了如何以角度 5 获取上一页 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Angular 5 应用程序,我需要知道如何获取最后一个 URL 以将其作为后退按钮的链接放置.我找到了这个 location.back() 但我需要最后一个 url 作为字符串.我怎样才能得到生成 location.back() 的字符串?

I'm working with Angular 5 app and I need to know how to get the last URL to place it as a link to my back button. I found this location.back() but what I need the last url as a string. How could I get the string that generates location.back()?

非常感谢!

推荐答案

Angular 6 更新了用于获取以前的 url 作为字符串的代码.

Angular 6 updated code for getting previous url as string.

import { Component, OnInit } from '@angular/core';
import { Router, RoutesRecognized } from '@angular/router';
import { filter, pairwise } from 'rxjs/operators';


export class AppComponent implements OnInit {

    constructor (
        public router: Router
    ) {
    }

    ngOnInit() {
        this.router.events
            .pipe(filter((e: any) => e instanceof RoutesRecognized),
                pairwise()
            ).subscribe((e: any) => {
                console.log(e[0].urlAfterRedirects); // previous url
            });
    }

这篇关于如何以角度 5 获取上一页 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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