Angular 6 router.events.filter 'filter' 在类型'Observable<Event>'上不存在 [英] Angular 6 router.events.filter 'filter' does not exist on type 'Observable<Event>'
本文介绍了Angular 6 router.events.filter 'filter' 在类型'Observable<Event>'上不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经完成将我的应用更新到 Angular 6(它是 5.2 版本).
我的语法错误:
import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';从'rxjs/operators'导入{过滤器};...构造函数(私有路由器:路由器){}this.router.events.filter(event => event instanceof NavigationEnd).subscribe((res) =>{//做点什么});
<块引用>
错误 TS2339:类型上不存在属性过滤器"'可观察'.
Angular 6 中正确的语法是什么?
谢谢
解决方案
这是如何使用 Angular 6+ 和最新的 RxJS 过滤路由器事件:
import { Component, OnInit } from '@angular/core';从@angular/router"导入{路由器、激活路由、导航结束};从'rxjs/operators'导入{过滤器};导出类 MyComponent 实现 OnInit {构造函数(私有路由器:路由器,私有激活路由:ActivatedRoute){}ngOnInit() {this.router.events.pipe(过滤器(事件 => NavigationEnd 事件实例)).subscribe(() => {console.log(this.activatedRoute.root);});}}
使用 pipe
运算符,而不是尝试在 observable 上进行链式过滤.
I have finished to update my App to Angular 6 (it was in 5.2 version).
I got an error syntax in :
import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';
import { filter } from 'rxjs/operators';
...
constructor(private router: Router) {}
this.router.events.filter
(event => event instanceof NavigationEnd).subscribe((res) =>
{
// DO something
});
error TS2339: Property 'filter' does not exist on type 'Observable'.
what's the right syntax in Angular 6 ?
thanks
解决方案
This is how to filter router events with Angular 6+ and latest RxJS:
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';
import { filter } from 'rxjs/operators';
export class MyComponent implements OnInit {
constructor(private router: Router, private activatedRoute: ActivatedRoute) {}
ngOnInit() {
this.router.events.pipe(
filter(event => event instanceof NavigationEnd)
).subscribe(() => {
console.log(this.activatedRoute.root);
});
}
}
Uses the pipe
operator instead of attempting to chain filter on the observable.
这篇关于Angular 6 router.events.filter 'filter' 在类型'Observable<Event>'上不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文