【发布时间】:2020-09-09 03:38:46
【问题描述】:
我知道你们中的许多人会说这是一个重复的问题,但我的担忧有所不同。我想在我的网页上显示从 jsonplaceholder.com 获得的虚假 API 的数据。我创建了一个使用该 API 的服务,以下是我的文件和错误。
error- TS2339: Property 'name' does not exist on type '{}'.
api.service.ts:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ApiService {
constructor(private _http:HttpClient) { }
names(): Observable<any>{
return this._http.get('https://jsonplaceholder.typicode.com/users');
}
}
component.ts:
import { Component, OnInit,Input } from '@angular/core';
import { MessageserviceService } from '../services/messageservice.service';
import { ApiService } from '../api.service';
import { Observable } from 'rxjs';
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.scss']
})
export class ChildComponent implements OnInit {
childdata;
itemname;
price:boolean=false;
list={};
constructor(private http:ApiService) { }
ngOnInit():any {
this.http.names().subscribe(data=> this.list=data)
}
}
component.html:
<ul>
<li>{{list.name}}</li>
</ul>
【问题讨论】:
-
list={}.name,不是列表(或数组)。此外,您似乎并不关心 API 响应的类型,请阅读angular.io/guide/http#requesting-a-typed-response
标签: angular typescript