我对角质还不熟悉。我试图获得一个简单的http请求并获得这个JSON:https://jsonplaceholder.typicode.com/users,具体来说,我只想循环名称。
在app.module.ts中,我添加了HttpClientModule:
import { HttpClientModule } from '@angular/common/http';在workers.component.ts中,这就是我所拥有的:
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-workers',
templateUrl: './workers.component.html',
styleUrls: ['./workers.component.css']
})
export class WorkersComponent implements OnInit {
showList = [];
http: HttpClient;
constructor() { }
ngOnInit() {
//THIS IS WHAT I TRIED
let obs = this.http.get('https://jsonplaceholder.typicode.com/users');
obs.subscribe(() => console.log('Got the response, yay.'));
//Later I would try to get the name with response[0].name
}
}workers.component.html非常简单:
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
</tr>
</thead>
<tbody>
<tr>
<td *nfFor="let name of names">{{name}}</td>
</tr>
</tbody>
</table>目前,我只得到一个错误:
ERROR TypeError:无法读取未定义的属性get( WorkersComponent.push../src/app/workers/workers.component.ts.WorkersComponent.ngOnInit (workers.component.ts:29) )
发布于 2018-08-23 10:28:33
发布于 2018-08-23 10:33:16
导入HttpClientModule
app.module.ts
imports: [
HttpClientModule
]workers.component.ts
constructor(private http: HttpClient) { }https://stackoverflow.com/questions/51983642
复制相似问题