【发布时间】:2021-06-11 10:46:20
【问题描述】:
我正在努力理解为什么我无法在使用 ngFor 的所有用户和表格中的组件的 html 中看到。在 usersList: User[] 中保留的接缝是 User 类型的数组列表的响应,如下图所示 PtrSCr of the Web page and the console error 但最后它说 错误:尝试比较时出错 '[{"id":101,"userName":"tcorneanu","password":"password","email":"tcorneanu@gmail.com"} ,{"id":104,"userName":"user3","password":"pwd3","email":"user3@gmail.com"}]'。 只允许使用数组和可迭代对象 HTML
<h2>User list</h2>
{{usersList}}
<table class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>index</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let hero of usersList; let i = index">
<td>{{hero.id}}</td>
<td>{{i}}</td>
</tr>
</tbody>
</table>
TS
usersList: User[] = [];
private getUsersList(token:any):void{
this.userService.getUsersList(token).subscribe(data => {
console.log("data is: "+data),
this.usersList = data
}, (error) => {
console.log(error);
});
}
用户服务
getUsersList(token:any): Observable<User[]>{
let tokenStr = 'Bearer '+token;
const headers = new HttpHeaders().set("Authorization",tokenStr);
return this.httpClient.get<User[]>(`${this.url}/users`,{headers, responseType: 'text' as 'json'});
}
用户
export interface User {
id: number;
userName: string;
password: string;
email: string;
}
【问题讨论】:
-
如果你这样做
console.log(data)会得到什么? -
你检查了
typeof data吗?也许它的 Response 对象,您需要使用data.json()获取用户数组 -
@Kenny 检查他的图片网址。他正在获取用户数组的纯字符串数据
-
恐怕您的数据类型可能是
string
标签: html angular typescript