Ano*_*ari 36 rxjs typescript angular angular6 rxjs6
我已经完成了将我的应用程序更新为Angular 6(它是在5.2版本中).
我有一个错误语法:
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
});
Run Code Online (Sandbox Code Playgroud)
错误TS2339:类型'Observable'上不存在属性'filter'.
Angular 6中的语法是什么?
谢谢
Lan*_*ara 90
这是使用Angular 6+和最新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);
});
}
}
Run Code Online (Sandbox Code Playgroud)
使用pipe运算符而不是尝试对可观察对象进行链式过滤.
如果您导入过滤器,我在您的代码中看不到
对于 Rxjs 6:
import { filter } from 'rxjs/operators';
.
.
.
this.router.events.pipe(
filter((event:Event) => event instanceof NavigationEnd)
).subscribe(res => console.log(res))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29707 次 |
| 最近记录: |