我有这样的服务方法:
updateUser(article) {
this.httpClient
.put(`articles/${article.id}`, article)
.pipe(
map(res => res.response),
mergeMap(updatedArticle =>
this.articlesTagsAPI.someMethod(updatedArticle.id).pipe(
map(tags => ({ ...updatedArticle, tags })),
article.articleType === 'news'
? mergeMap(() =>
this.articlesTagsAPI.someOtherMethod(updatedArticle.id, article.tags).pipe(
map(() => {
return {
...updatedArticle,
tags: article.tags
};
})
)
)
: null
)
)
);
}正如你所看到的-我正在尝试添加一个条件mergeMap (如果条件->调用另外一个方法),有没有可能这样做?
因为返回null不是最好的主意
如果conditional无效,应该会发生这样的事情:
updateUser(article) {
this.httpClient
.put(`articles/${article.id}`, article)
.pipe(
map(res => res.response),
mergeMap(updatedArticle =>
this.articlesTagsAPI.someMethod(updatedArticle.id).pipe(
map(tags => ({ ...updatedArticle, tags }))
)
)
);
}发布于 2018-12-10 21:18:50
正如在这个非常有用的订阅中所述,正确的方法是添加of指令,以便始终在最终的订阅中返回一个可观察对象的值。
试试这个:
updateUser(article) {
this.httpClient
.put(`articles/${article.id}`, article)
.pipe(
map(res => res.response),
mergeMap(updatedArticle =>
this.articlesTagsAPI.someMethod(updatedArticle.id).pipe(
map(tags => ({ ...updatedArticle, tags })),
article.articleType === 'news'
? mergeMap(() =>
this.articlesTagsAPI.someOtherMethod(updatedArticle.id, article.tags).pipe(
map(() => {
return {
...updatedArticle,
tags: article.tags
};
})
)
)
: of(null)
)
)
);
}发布于 2020-09-28 00:05:15
试试这个:
updateUser(文章){
this.httpClient
.put(`articles/${article.id}`, article)
.pipe(
map(res => res.response),
mergeMap(() =>
this.articlesTagsAPI.someOtherMethod(updatedArticle.id,
article.tags).pipe(
map(() => {
if(article.articleType === 'news'){
return {
...updatedArticle,
tags: article.tags
};
}else{ return of([])}
})
)
)
)
)
);}
https://stackoverflow.com/questions/53705994
复制相似问题