将rxjs管道封装成函数
我一直在搜索,但找不到答案。
有没有办法将RXJS管道方法封装到自定义方法中?
<observable>.pipe(
filter((num: number) => num % 2 === 0),
take(10)
map((num: number) => num * 10),).subscribe(...);
我想将其简化为
<observable>.doSomeThingsThatArePiped().subscribe(...);
可自定义的可管道操作员以简化管道中的内容,但是在我的代码中,我有一组我使用的操作员多次使用,并希望尽可能多地减少它 - 包括管道。
I've been searching, but cannot find an answer to this.
Is there a way to encapsulate an rxjs pipe method into a custom method?
<observable>.pipe(
filter((num: number) => num % 2 === 0),
take(10)
map((num: number) => num * 10),).subscribe(...);
I'd like to reduce it to
<observable>.doSomeThingsThatArePiped().subscribe(...);
I know about custom pipeable operators to simplify what's in the pipe, but in my code I have a set of operators that I use multiple times and would like to reduce it down as much as possible - including the pipe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
并像这样使用它:
And use it like this:
您可以将通用链封装在
管道
之类的中:然后用法很简单:
you can encapsulate common chains in
pipe
like so:then usage is simple:
我的原始代码是通用的,但这是我为扩展 Observable 原型的真实项目想出的代码。
My original code was made generic, but here's what I've come up with for my real project that extends the Observable prototype.