订阅不是功能-离子4

时间:2019-03-23 11:28:08

标签: angular api ionic-framework ionic4

我试图在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

1 个答案:

答案 0 :(得分:2)

您已将http get observable分配给this.item字段。在下一行中,您应该使用this.item而不是this.items进行订阅,并且可能将诸如this.items = data之类的结果保存在订阅中,以便以后处理。