首页 文章

Angular 5 HTTPclient json错误

提问于
浏览
0

我试图从我的Angular 5离子项目中的json文件导入数据,但我不能因为我收到错误: TS2339: property results does not exist on type 'Object' 这是在home.ts(其中2个)

提供者: people.ts

getPeople() {
    return this.http.get('http://www.json-generator.com/api/json/get/cghlMAGJvm?indent=10')
  }
}

home.ts

constructor(public navCtrl: NavController,
              public service: People)
  {
    this.service.getPeople()

      .subscribe(
        data => this.people = data.results
      )
  }

  toggleReorder(){
    this.shouldReorder = !this.shouldReorder
  }
doRefresh(e){
this.service.getPeople()
  .subscribe(
    data => this.people.unshift(...data.results),
    err => console.log(err),
    () => e.complete()
  )
}
}

正如我在angular / http指南中看到的那样应该是这样的:

showConfig() {
  this.configService.getConfig()
    .subscribe(data => this.config = {
        heroesUrl: data['heroesUrl'],
        textfile:  data['textfile']
    });
}

But I want to copy all the data not only few parts. What is the right way of doing it ?

1 回答

  • 1

    people.ts 上为 Map 运算符添加导入 .

    import 'rxjs/add/operator/map';
    
    
    getPeople() {
        return this.http.get('http://www.json-generator.com/api/json/get/cghlMAGJvm?indent=10').map(res => res.json());
    }
    

    http://www.json-generator.com/api/json/get/cghlMAGJvm?indent=10给出的响应已经是结果,因此无需添加 data.results ,响应中不存在该属性 .

    this.service.getPeople().subscribe( data => {
        this.people = data
    });
    

相关问题