订阅不是一个功能 - ionic 4
subscribe is not a function - ionic 4
我正在尝试在我的 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
您已将 http get observable 分配给 this.item
字段。在下一行中,您应该使用 this.item
而不是 this.items
来订阅它,并且可能将结果保存在订阅内部,如 this.items = data
以便稍后处理。
我正在尝试在我的 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
您已将 http get observable 分配给 this.item
字段。在下一行中,您应该使用 this.item
而不是 this.items
来订阅它,并且可能将结果保存在订阅内部,如 this.items = data
以便稍后处理。