问题描述
我有一个订阅,可以从NgRx减速器获取提供程序.我想使用takeUntil()
自动关闭订阅,直到最终返回具有内容的数组:
I have a subscription to get providers from a NgRx reducer. I want to use takeUntil()
to automatically close the subscription when is finally returns an array that has content:
// Fetch providers
this.store.pipe(select(reducer.getProviders))
// .takeUntil(reducer.getProviders.length > 0)
.subscribe(providers => {
if (providers) {
this.providers = providers;
// takeUntil(/** something **/);
}
});
有人可以帮我吗?
我不知道如何使用takeUntil()
推荐答案
takeUntil
接受一个Observable. (来源:文档).对于您的情况,使用takeWhile
更有意义,只要满足特定条件,它就会发出值(来源:文档).将可选的inclusive
属性设置为true,以便它还会发出未通过谓词的第一项.
takeUntil
accepts an Observable. (Source: docs). For your case, it would make more sense to use takeWhile
, this will emit values as long as a particular condition is satisfied (Source: docs). Set the optional inclusive
property to true so that it will also emit the first item that didn't pass the predicate.
this.store.pipe(select(reducer.getProviders))
.takeWhile(reducer => reducer.getProviders.length == 0, true)
.subscribe(providers => {
if (providers) {
this.providers = providers;
}
});
这篇关于使用takeUntil()关闭条件的订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!