gpt4 book ai didi

javascript - Angular 2 中带有两个 http.get 调用的可观察类型

转载 作者:行者123 更新时间:2023-11-30 09:43:50 26 4
gpt4 key购买 nike

在我的 ng2 服务中,我有一个有 2 个 http.get 调用的方法。该函数看起来像这样:

getInfo(userId: number): any {

this.http
.get(apiUrl, options)
.map(response => response.json())
.subscribe(example => {
this.example.FirstName = example.FirstName;
this.example.LastName = example.LastName;

this.http
.get(`/api/${userId}`)
.map(response => response.json())
.subscribe(example => {
this.example.Street = example.Street;
this.example.City = example.City;

return this.example;
});
});
}

唯一的问题是,在我的组件中,我无法订阅这个函数,因为它不是 Observable<Example> 类型的.

如果我用 Observable<Example> 替换函数的类型 any我得到:

声明类型既不是“void”也不是“any”的函数必须返回一个值

但我确实在响应后返回了一个值。

如果没有两个独立的函数,我该如何做到这一点?

是的,我确实检查了这个答案:https://stackoverflow.com/a/36712707/3264998

最佳答案

试着把它写成你的方法体,这是解决这个问题的一种方法。

return this.http
.get(apiUrl, options)
.map(response => response.json())
.flatMap(example => {
this.example.FirstName = example.FirstName;
this.example.LastName = example.LastName;

return this.http
.get(`/api/${userId}`)
.map(response => {
let example =response.json();
this.example.Street = example.Street;
this.example.City = example.City;

return this.example;
});
});

关于javascript - Angular 2 中带有两个 http.get 调用的可观察类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39851104/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com