【发布时间】:2018-10-31 23:05:41
【问题描述】:
在 Angular 5 中,我想使用 first() 方法,如下所示:
this.ccService.mode.first().subscribe(mode => {
this.mode = mode;
});
我是这样导入的:import { first } from 'rxjs/operators/first';。
我也尝试过从'rxjs/add/operator'、'rxjs/operators'、'rxjs' 导入,似乎都没有。
但是,它拒绝工作,只给我你已经在标题中看到的错误消息:[ts] Property 'first' does not exist on type 'Observable<string>'.。
mode 可观察:
private modeSource = new BehaviorSubject<string>('new');
public mode = this.modeSource.asObservable();
public setMode(mode: string) {
this.modeSource.next(mode);
}
我一直在谷歌搜索,但我似乎找不到有同样错误的人,我只是没有使用first() 对吗?我应该使用.pipe(first()).subscribe 吗? https://www.learnrxjs.io/operators/filtering/first.html 断断续续地使用observable.first().subscribe 和observable.pipe(first()).subscribe 没有明确的解释或推理,所以我在这里有点迷失了。
【问题讨论】:
-
使用 RxJS 5 和“原型”风格的运算符,您需要使用
import 'rxjs/add/operator/first';修补 Observable 类 -
@martin 修复了它,你应该让它成为答案而不是评论
标签: angular typescript rxjs