在以下代码中: -
RxJS.Observable.of(1,2).first().subscribe((x) => console.log(x););
是否有必要取消订阅操作员first()
?
答案 0 :(得分:7)
不。调用first()
后,它将自动退订。对于RxJS 6,当前语法为observable.pipe(first()).subscribe(func);
。
答案 1 :(得分:2)
对于提供的示例,您不需要unsubscribe
,也不需要拨打first
,因为Observable.of(1)
实际完成后发出(最后一个)值。
答案 2 :(得分:1)
first()
将在从observable发出第一个项目后完成。
同样subscribe()
有三个参数,最后一个是完整的回调。运行以下代码将输出1,然后输出'done'。
Rx.Observable.of(1)
.subscribe(
(x) => console.log(x), // next
(x) => console.error(x), // error
() => console.log('done') // done
)