【发布时间】:2019-09-10 22:52:42
【问题描述】:
我正在尝试通过服务从 API 获取数据。然后从模块内部记录它,该模块稍后将在列表中显示它。该变量显示在服务的 console.log 中,但当我尝试将其记录到 de 模块时却没有显示
我之前使用 ionic 进行的相同设置,我认为 angular 的工作方式相同,但这里情况并非如此。我已经使用错误处理扩展了 .subscribe 函数,但没有出现错误。我只是不知道去哪里找。
api.service.ts
getPlacesData(): any {
let result = this.http.get(`${this.apiUrl}/places`, this.httpOptions).pipe(
map(response => {
console.log(response['data']);
response['data'];
})
);
return result;
}
}
test-dashboard.component.ts
constructor(private breakpointObserver: BreakpointObserver, private apiService: ApiService) {}
getPlacesData() {
this.apiService.getPlacesData().subscribe(
(placesData: Observable<any>) => {
console.log(placesData);
this.placesData = placesData;
console.log(this.placesData);
},
(error: string) => {
console.log('Received an errror: ' + error);
}
);
}
ngOnInit() {
this.getPlacesData();
}
}
我希望第二个和第三个 console.log 具有与第一个相同的输出,但它们的输出是“未定义”而不是:
(15) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
【问题讨论】:
-
您需要确保在 map() 中实际返回响应,目前您没有隐式或显式返回任何内容。
-
您目前在地图函数中没有返回任何内容
标签: angular typescript angular-material