【发布时间】:2019-02-24 04:51:42
【问题描述】:
我正在使用 Angular RxJs subscribe 进行 HttpClient 调用,然后使用第一个调用中的值进行另一个调用。在这种情况下,调用获取address object,然后我使用此对象进行调用。像这样:
@Injectable()
export class AddressService {
constructor(private http: HttpClient) { }
getById(addressId: string, userId: string) {
return this.http.get(BACKEND_URL + 'getAddressById/' + [addressId, userId]);
}
}
export class AddressModalComponent implements OnInit {
constructor(private alertService: AlertService, private addressService: AddressService, @Inject(MAT_DIALOG_DATA) public data: any, private dropdownService: DropdownService)
ngOnInit() {
this.addressService.getById(this.data.id, this.data.userId)
.subscribe(
(address: Address) => {
this.dropdownService.getCidadesBrByEstado(address.name)
.subscribe((cities: BrCity[]) => {
this.cities = cities;
this.address = address;
},
error => console.log(error));
}, error => { this.alertService.error(error);
}
);
}
}
}
我试图避免多次订阅,我的代码中有很多这样的。我需要像Node.js promises 这样的Async/Await 方法,但在组件级别使用Observables。我对RxJs commands 不是很熟悉...有没有更好的方法可以只用一个subscribe 和catch 拨打多个电话?
【问题讨论】:
标签: angular typescript rxjs observable