如何从我的 Angular 操作访问 API 调用的“响应”的 属性?
How do I access a property of the “response” of an API call from my Angular operation?
我在 NODE.JS 中制作了一个 API,它将一个对象作为它的响应,我在下面发布了它的图像。
这是我的 users.component.ts 文件中的打字稿代码。
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'users',
templateUrl: './users.component.html',
styleUrls: ['./users.component.css']
})
export class UsersComponent {
users: any[];
constructor(http: HttpClient) {
http.get('url').subscribe(response => {
console.log(response.recordset);
this.users=response.recordset;
});
}
}
这是我收到的错误消息:
src/app/users/users.component.ts:14:28 - error TS2339: Property 'recordset' does not exist on type 'Object’.
14 console.log(response.recordset);
src/app/users/users.component.ts:15:27 - error TS2339: Property 'recordset' does not exist on type 'Object'.
15 this.users=response.recordset;
如何访问此“记录集”属性 并将其添加到用户数组中?
如果您不知道返回响应的响应类型,可以通过添加 any
来避免错误,这将为您解决问题。
不过,最佳做法是采用响应类型,
http.get('url').subscribe((response : any) => {
我在 NODE.JS 中制作了一个 API,它将一个对象作为它的响应,我在下面发布了它的图像。
这是我的 users.component.ts 文件中的打字稿代码。
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'users',
templateUrl: './users.component.html',
styleUrls: ['./users.component.css']
})
export class UsersComponent {
users: any[];
constructor(http: HttpClient) {
http.get('url').subscribe(response => {
console.log(response.recordset);
this.users=response.recordset;
});
}
}
这是我收到的错误消息:
src/app/users/users.component.ts:14:28 - error TS2339: Property 'recordset' does not exist on type 'Object’.
14 console.log(response.recordset);
src/app/users/users.component.ts:15:27 - error TS2339: Property 'recordset' does not exist on type 'Object'.
15 this.users=response.recordset;
如何访问此“记录集”属性 并将其添加到用户数组中?
如果您不知道返回响应的响应类型,可以通过添加 any
来避免错误,这将为您解决问题。
不过,最佳做法是采用响应类型,
http.get('url').subscribe((response : any) => {