Angular:如何将管道与位置结合使用

问题描述 投票:0回答:1

在 Angular 中,Location“应用程序可用于与浏览器 URL 交互的服务。”

它提供了一个

getState()
方法,该方法提供 “位置历史记录的当前状态” 和一个
subscribe()
方法,以便人们可以订阅 “平台的
popState
事件。”
.

这些都很有用,但是我想知道如何访问位置的可观察值,以便我可以

.pipe
到它并在使用
.subscribe
之前使用一些rxjs运算符

angular typescript rxjs rxjs-pipeable-operators
1个回答
0
投票

可能不是最干净的解决方案,但由于位置 API 不提供任何内容,为什么不呢:

订阅路由器,映射到位置状态:


  ngOnInit() {
    const stateObservable = this.router.events.pipe(
      filter(event => event instanceof NavigationEnd),
      map(() => this.location.getState())
    );
  }

© www.soinside.com 2019 - 2024. All rights reserved.