我有这种服务方法:
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-不是最好的主意
如果条件无效,应该发生以下情况:
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 }))
)
)
);
}
答案 0 :(得分:1)
如此非常有用的article中所述,执行此操作的正确方法是添加of
指令,以始终在最终订阅中返回Observable的值。
尝试一下:
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)
)
)
);
}
答案 1 :(得分:0)
尝试一下:
updateUser(文章){
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
COPY_PHASE_STRIP = NO;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "MyApp/MyApp-Prefix.pch";
GCC_THUMB_SUPPORT = NO;
GCC_VERSION = "";
INFOPLIST_FILE = "MyApp/MyApp-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = io.ionic.starter;
PRODUCT_NAME = "$(TARGET_NAME)";
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
1D6058950D05DD3E006BFB54 /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 3047A5101AB8059700498E2A /* build-release.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
COPY_PHASE_STRIP = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "MyApp/MyApp-Prefix.pch";
GCC_THUMB_SUPPORT = NO;
GCC_VERSION = "";
INFOPLIST_FILE = "MyApp/MyApp-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = io.ionic.starter;
PRODUCT_NAME = "$(TARGET_NAME)";
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
};
C01FCF4F08A954540054247B /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 3047A5111AB8059700498E2A /* build.xcconfig */;
buildSettings = {
...
WK_WEB_VIEW_ONLY = 1;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
};
name = Debug;
};
.....
}