muturgan Asked:2020-07-20 05:12:57 +0800 CST2020-07-20 05:12:57 +0800 CST 2020-07-20 05:12:57 +0800 CST Angular如何知道以前的路线? 772 我想在组件的 OnInit 挂钩中执行某个操作,具体取决于用户来自哪个路由。我怎样才能找到上一条路线的名称(或其他一些标识符)? маршрутизация 2 个回答 Voted Best Answer overthesanity 2020-07-22T01:40:45+08:002020-07-22T01:40:45+08:00 如果你使用rxjs > 5.5,那么有所谓的converts to的支持pipeable operators,当然会有一个错误,即. 你不能一个人做,因为它有自己的生命周期,作为一个选项,使用更多:pipeObservableStream node.jsproperty pairwise does't exist on type Observable<Event>pairwiseRouterNavigationStart => NavigationEndfilter import { Router, RouterEvent, NavigationEnd } from '@angular/router'; import { filter, pairwise } from 'rxjs/operators'; ..... @Component({ ... }) class SomeComponent implements OnInit { constructor(private router: Router) {} public ngOnInit(): void { this.router.events.pipe<RouterEvent>( filter((event) => event instanceof NavigationEnd), pairwise() ).subscribe(([previous, current]: [NavigationEnd, NavigationEnd]) => { console.log(previous.url); console.log(current.url); }); } } Roman C 2020-07-20T05:33:00+08:002020-07-20T05:33:00+08:00 使用对运算符来获取先前和当前事件,这是一个好主意。 this.router.events.pairwise().subscribe((event) => { ... });
如果你使用
rxjs > 5.5
,那么有所谓的converts to的支持pipeable operators
,当然会有一个错误,即. 你不能一个人做,因为它有自己的生命周期,作为一个选项,使用更多:pipe
Observable
Stream
node.js
property pairwise does't exist on type Observable<Event>
pairwise
Router
NavigationStart => NavigationEnd
filter
使用对运算符来获取先前和当前事件,这是一个好主意。