我将我的角度升级到 ng7。我正在做一个项目,我有 3 个 http 调用,它们相互依赖,所以我选择使用 switchMap。我的问题是,我之前编写 switchMap 的方式不起作用。这是我平时写的
DeleteConfirm(id: number, $event) {
console.log('product');
this.productService.deleteProductById(
id).switchMap(productDeleted => this.productService.getProducts())
.subscribe(
products => {
this.products = products;
this.confirmDelete = false;
}, error2 => {}
);
$event.stopPropagation();
}
下面的方法是我现在正在研究的方法。关于我应该如何使用 switchMap 的任何建议?
createCompWithGroup(competitionName: string) {
return this.apiService.createACompetition(competitionName)
.pipe(switchMap(data => {
const competition = data['category'];
const competitionSlug = competition.id + '-' + competition.slug;
this.createSecurityGroup(competitionSlug).subscribe( data =>{
return this.addSecurityGroup(competitionName, competitionSlug)
}
}
));
}