[英]takeUntil does not work with BehaviorSubject
I have the following BehaviorSubject defined: 我定义了以下BehaviorSubject:
private posts = new BehaviorSubject<any[]>([]);
and on init: 并在初始化:
ngOnInit() {
this.posts
.takeUntil(!this._postsLoaded)
.subscribe(x => {
this._postsLoaded = true;
// do something
});
}
But it gets the following error, although it should work: 但是它会出现以下错误,尽管它应该可以工作:
Property 'takeUntil' does not exist on type 'BehaviorSubject<any>'
If you want to use like a chain syntax , You have to install rxjs compat also for backward comptability. 如果要像链式语法那样使用,则还必须安装rxjs compat以便向后兼容。
npm install --save rxjs-compat
But I would suggest use pipes 但我建议使用管道
import { takeUntil } from 'rxjs/operators';
ngOnInit() {
this.posts.pipe(
takeUntil(!this._postsLoaded)
).subscribe(x => {
this._postsLoaded = true;
// do something
};
}
Sample Stackblitz Sample Stackblitz
https://stackblitz.com/edit/rxjs-takeuntilexample?file=index.ts https://stackblitz.com/edit/rxjs-takeuntilexample?file=index.ts
There're two things: 有两件事:
In RxJS 6 you should use pipable operators. 在RxJS 6中,应使用可点运算符。 If you have to use the old "patch" style of operators you'll need to include
rxjs-compat
package 如果必须使用旧的“修补程序”样式的运算符,则需要包括
rxjs-compat
包
The takeUntil()
operator takes as a parameter another Observable so what you have now will throw an error anyway. takeUntil()
运算符将另一个Observable用作参数,因此无论如何您现在将抛出错误。
Maybe you should use takeWhile()
instead. 也许您应该改用
takeWhile()
。 See this answer ( RxJS takeWhile but include the last value ) if you want to also include the last value that completed the chain. 如果您还想包含完成链的最后一个值, 请参见此答案( RxJS takeWhile但包括最后一个值 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.