Angular6更新了获取前一个URL作为字符串的代码。

    1. import { Component, OnInit } from '@angular/core';
    2. import { Router, RoutesRecognized } from '@angular/router';
    3. import { filter, pairwise } from 'rxjs/operators';
    4. export class AppComponent implements OnInit {
    5. constructor (
    6. public router: Router
    7. ) {
    8. }
    9. ngOnInit() {
    10. this.router.events
    11. .pipe(filter((e: any) => e instanceof RoutesRecognized),
    12. pairwise()
    13. ).subscribe((e: any) => {
    14. console.log(e[0].urlAfterRedirects); // previous url
    15. });
    16. }