在一个离子项目中,我尝试通过使用httpclient从API获取数据,但Angular将我提高了属性'名称'在类型'{}}上不存在。误。有组件和提供商方法的代码。
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Storage } from '@ionic/storage';
export class CreerDiscussionPage {
discussion: Discussion;
constructor(
public navCtrl: NavController,
public navParams: NavParams,
public rfcAPI: RfcApiProvider,
public discussionProvider: DiscussionProvider
) {
this.rfcAPI = rfcAPI;
this.discussionProvider = discussionProvider;
this.discussion = new Discussion();
}
getDiscussion() {
this.rfcAPI.getDiscussion()
.then(data => {
// Error raised in this place : Property 'name' does not exist on type '{}'.
console.log(data.name);
});
}
}
// Method in the rfcApi provider
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
.....
getDiscussion() {
return new Promise(resolve => {
this.http.get('http://localhost:3000/api/conversation/9a19d9e944e4b105d360614572b715434a365e724beddb83a4f1464a1ea79d29')
.subscribe(data => {
resolve(data);
}, err => {
console.log(err);
});
});
}
您知道一种定义 data> data 变量类型的方法吗?还是解决我的问题的其他方法?
.then((data: any) => {
console.log(data.name);
});
它可能是这样起作用的吗?
我也认为您可以在服务中执行此操作
return this.http.get('http://localhost:3000/api/conversation/9a19d9e944e4b105d360614572b715434a365e724beddb83a4f1464a1ea79d29').toPromise()