我试图在ionic 4项目中使用api获取一些数据。我遵循了本指南:
https://ionicacademy.com/http-calls-ionic/
,这里是我的list.page.ts
:
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-list',
templateUrl: 'list.page.html',
styleUrls: ['list.page.scss']
})
export class ListPage implements OnInit {
private selectedItem: any;
private icons = [
'flask',
'wifi',
'beer',
'football',
'basketball',
'paper-plane',
'american-football',
'boat',
'bluetooth',
'build'
];
public items: Array<{ title: string; note: string; icon: string }> = [];
items: Observable<any>;
constructor(public httpClient: HttpClient){
this.item = this.httpClient.get('http://www.fostania.com/api/items/');
this.items.subscribe(data => {
console.log('my data: ', data);
})
}
ngOnInit() {
}
// add back when alpha.4 is out
// navigate(item) {
// this.router.navigate(['/list', JSON.stringify(item)]);
// }
}
当我尝试访问此页面时,出现错误:
core.js:15723 ERROR Error: Uncaught (in promise): TypeError: this.items.subscribe is not a function
答案 0 :(得分:2)
您已将http get observable分配给this.item
字段。在下一行中,您应该使用this.item
而不是this.items
进行订阅,并且可能将诸如this.items = data
之类的结果保存在订阅中,以便以后处理。