【发布时间】:2019-04-28 15:09:22
【问题描述】:
您好,我正在尝试从 Angular 的 get 请求中获取数据。我有一个看起来像这样的班级公寓:
export class Apartment {
id: number;
address: string;
constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
}
一项服务
@Injectable({
providedIn: 'root'
})
export class Service {
baseURL = 'http://localhost:3000';
constructor(private httpClient: httpClient) {}
getApartments() {
return this.httpClient.get(this.baseURL + '/apartments');
}
}
一个组件
export class SettingsComponent implements OnInit {
apartments: Apartment[] = [];
constructor(private apiService: Service) {
// todo: fetch data here...
}}
我有一个可以工作的 rest api 和一个可以读取的 json 文件,如下所示:
{
"id" : 0,
"address: "not assinged yet"
}
如何读取数据并将其转换为 Apartment 数组?
【问题讨论】: