问题描述
例如,我有两个嵌套的订阅方法。有时我有两个以上的订阅方法,它们需要连接,所以当第一个结束时第二个应该开始等... 什么是最好的解决方案,以便他们可以等待其他解决方案,但又不想以这种方式嵌套呢?
this.userService.postUser(usermodel).subscribe(postUser => {
this.userService.getAllUsers().subscribe(users => {
})
})
解决方法
如果它们需要按顺序运行,则可以使用switchMap
运算符:
this.userService
.postUser(userModel)
.pipe(switchMap((postUser /* unused? */) => this.userService.getAllUsers()))
.subscribe(users => { })