不能在使用可观察对象的函数中使用 return 语句
cannot use a return statement in a function that uses observables
所以我刚刚了解到我不能在使用异步操作的函数中使用 return 语句,或者我可以但我使用不正确。我的想法是,我应该只在我想使用我正在抓取的数据的地方订阅 observable,而不是仅仅在那个地方调用函数并使用 return 语句。
这是我的代码什么是更好的做事方式所以我不必使用 return 语句,或者更好的是,我如何仍然使用 return 语句.请注意我想使用 observables,而不是 promises,我不再看到 promises 的价值。
谢谢。
getService(url) {
var value: any[] = [];
this.http.get(url)
.map(
(response: Response) => {
const data = response.json()
.subscribe(
(mappedData: any[]) => value = mappedData,
(error) => console.log(error)
)
}
);
return value;
}
我们可以通过这种方式 return json 对象的值。
getService(url) {
var value: any[] = [];
var subscription = this.http.get(url)
.map(response => response.json() )
.subscribe(
mappedData => value = mappedData.data,
(error) => console.log(error)
);
return value; }
- 使用
.map()
将 http.get()
的响应转换为 json 对象
- 然后
.subscribe()
到 .map() 的输出,它是转换后的 json 对象
- 最后从订阅的数据中设置
value
.. return 值
请仔细阅读 angular documentation 以便更好地理解。
据我了解:
getService(url) {
var value: any[] = []; // (1)
this.http.get(url) // (2)
.map(response => response.json() ) // (3)
.subscribe( // (4)
mappedData => value = mappedData.data, // (5)
(error) => console.log(error) // (6)
);
return value; // (7)
}
当调用getService()
函数时...
第 1 行执行并将值设置为空数组。
第2行执行,执行Http get操作并设置Observable。
第 7 行执行,返回现在仍然为空的数组。
在稍后的某个时间点,当从服务器返回响应时...
第 3 行执行将响应映射到 JSON 对象并将其提供给订阅方法
第 5 行执行,将本地值 属性 分配给 mappedData 的数据 属性,因为外部函数已经返回 属性 ...结果值没有做任何事情。
这就是我对这段代码的理解。如有不妥请指正
先前答案中引用的文档实际上显示了这样的代码:
在名为 HeroService
的 Angular 服务中:
getHeroes(): Observable<Hero[]> {
return this.http.get(this.heroesUrl)
.map(this.extractData)
.catch(this.handleError);
}
请注意,这是返回 Observable ……而不是 属性。并且组件中的调用代码包含订阅方法:
在名为 HeroComponent
的 Angular 组件中:
getHeroes() {
this.heroService.getHeroes()
.subscribe(
heroes => this.heroes = heroes,
error => this.errorMessage = <any>error);
}
getService(url) {
var value: any[] = [];
var subscription = this.http.get(url)
.map(response => response.json() )
subscription.subscribe(
mappedData => value = mappedData.data,
(error) => console.log(error)
);
return value; }
所以我刚刚了解到我不能在使用异步操作的函数中使用 return 语句,或者我可以但我使用不正确。我的想法是,我应该只在我想使用我正在抓取的数据的地方订阅 observable,而不是仅仅在那个地方调用函数并使用 return 语句。
这是我的代码什么是更好的做事方式所以我不必使用 return 语句,或者更好的是,我如何仍然使用 return 语句.请注意我想使用 observables,而不是 promises,我不再看到 promises 的价值。
谢谢。
getService(url) {
var value: any[] = [];
this.http.get(url)
.map(
(response: Response) => {
const data = response.json()
.subscribe(
(mappedData: any[]) => value = mappedData,
(error) => console.log(error)
)
}
);
return value;
}
我们可以通过这种方式 return json 对象的值。
getService(url) {
var value: any[] = [];
var subscription = this.http.get(url)
.map(response => response.json() )
.subscribe(
mappedData => value = mappedData.data,
(error) => console.log(error)
);
return value; }
- 使用
.map()
将 - 然后
.subscribe()
到 .map() 的输出,它是转换后的 json 对象 - 最后从订阅的数据中设置
value
.. return 值
http.get()
的响应转换为 json 对象
请仔细阅读 angular documentation 以便更好地理解。
据我了解:
getService(url) {
var value: any[] = []; // (1)
this.http.get(url) // (2)
.map(response => response.json() ) // (3)
.subscribe( // (4)
mappedData => value = mappedData.data, // (5)
(error) => console.log(error) // (6)
);
return value; // (7)
}
当调用getService()
函数时...
第 1 行执行并将值设置为空数组。
第2行执行,执行Http get操作并设置Observable。
第 7 行执行,返回现在仍然为空的数组。
在稍后的某个时间点,当从服务器返回响应时...
第 3 行执行将响应映射到 JSON 对象并将其提供给订阅方法
第 5 行执行,将本地值 属性 分配给 mappedData 的数据 属性,因为外部函数已经返回 属性 ...结果值没有做任何事情。
这就是我对这段代码的理解。如有不妥请指正
先前答案中引用的文档实际上显示了这样的代码:
在名为 HeroService
的 Angular 服务中:
getHeroes(): Observable<Hero[]> {
return this.http.get(this.heroesUrl)
.map(this.extractData)
.catch(this.handleError);
}
请注意,这是返回 Observable ……而不是 属性。并且组件中的调用代码包含订阅方法:
在名为 HeroComponent
的 Angular 组件中:
getHeroes() {
this.heroService.getHeroes()
.subscribe(
heroes => this.heroes = heroes,
error => this.errorMessage = <any>error);
}
getService(url) {
var value: any[] = [];
var subscription = this.http.get(url)
.map(response => response.json() )
subscription.subscribe(
mappedData => value = mappedData.data,
(error) => console.log(error)
);
return value; }