【问题标题】:How to get the previous page URL in angular 10如何在 Angular 10 中获取上一页 URL
【发布时间】:2020-09-06 05:59:46
【问题描述】:

如何获取 Angular 10 中的上一页 url?我正在尝试使用下面帖子中最初提到的过滤器、成对和路由器 RoutesRecognized

How to determine previous page URL in Angular?

第一次不返回任何值,第二次返回,第三次返回值 2 次,下次递增。

如何从第一次获得确切的值,并且控制台每次返回时应该只在此页面中返回一次?

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

@Component({
  selector: 'app-a',
  templateUrl: './a.component.html',
  styleUrls: ['./a.component.css']
})
export class AComponent implements OnInit {

  constructor(
    private router: Router
  ) { }

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

【问题讨论】:

    标签: angular


    【解决方案1】:

    您可以使用事件 instanceof NavigationEnd 并创建一个服务来持久化 currentUrl 并将其分配回任何路由器 NavigationEnd 事件上的先前 url。

    我创建了一个 Stackblitz,其中包含一个用例的快速演示:https://stackblitz.com/edit/angular-xw94et?file=src/app/app.component.html

    【讨论】:

      猜你喜欢
      • 2011-10-20
      • 2016-05-20
      • 2020-08-18
      • 2020-03-17
      • 2019-12-05
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      • 2018-11-25
      相关资源
      最近更新 更多