angular - 映射 https 响应时获取 [object Object]
问题描述
在角度 10 中使用 Observable 地图时,我得到了一个 [object Object]。
这是来自 API 服务的响应对象。
{
"result": {
"subject": {
"name": "ROBERT JAZGARA",
"nip": "7272445205",
"statusVat": "Czynny",
"regon": "472301670",
"pesel": null,
"krs": null,
"residenceAddress": "ZAGŁOBY 21/10, 02-495 WARSZAWA",
"workingAddress": null,
"representatives": [],
"authorizedClerks": [],
"partners": [],
"registrationLegalDate": "2002-05-21",
"registrationDenialBasis": null,
"registrationDenialDate": null,
"restorationBasis": null,
"restorationDate": null,
"removalBasis": null,
"removalDate": null,
"accountNumbers": [
"95213000042001029002150001",
"78105019241000009719369622"
],
"hasVirtualAccounts": false
},
"requestId": "mbn71-88a34h6",
"requestDateTime": "27-07-2020 23:52:30"
}
}
这是我创建的用于映射响应的模型。
export interface VatAPI {
result: EntityResponse;
}
export interface EntityResponse {
subjects: Entity[];
requestDateTime: string;
requestId: string;
}
export interface Entity {
authorizedClerks: EntityPerson[];
regon: string;
workingAddress: string;
hasVirtualAccounts: boolean;
statusVat: string;
krs: string;
restorationBasis: string;
accountNumbers: string[];
registrationDenialBasis: string;
representatives: EntityPerson[];
residenceAddress: string;
registrationDenialDate: Date;
restorationDate: Date;
name: string;
registrationLegalDate: Date;
removalBasis: string;
removalDate: Date;
nip: string;
partners: EntityPerson[];
pesel: string;
}
export interface EntityPerson {
firstName: string;
lastName: string;
nip: string;
companyName: string;
}
服务类调用 https 并映射响应。
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import {
HttpClientModule,
HttpClient,
HttpParams,
} from '@angular/common/http';
@Injectable()
export class ApiService {
constructor(private httpclient: HttpClient) {}
getcomments(): Observable<any> {
return this.httpclient.get(
'https://wl-api.mf.gov.pl/api/search/nip/7272445205?date=2020-07-27'
);
}
}
这是调用 api.service.ts 的 app.component.ts
import { Component } from '@angular/core';
import { VatAPI } from './classes/vatapi';
import { ApiService } from './services/api.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
constructor(private ApiService: ApiService) {}
lstresult: VatAPI;
// tslint:disable-next-line: use-lifecycle-interface
ngOnInit(): void {
this.ApiService.getcomments().subscribe((data) => {
this.lstresult = data;
});
}
}
我的 app.comonent.html 看起来像这样
<h2>Get Request (https://wl-test.mf.gov.pl:9091/wykaz-podatnikow/api/search/nip/7272445205?date=2020-07-27)</h2>
<span>{{ lstresult }}</span>
当我为项目提供服务时,我得到了 [object Object] 响应。我无法弄清楚错误在哪里请帮助
解决方案
使用json 管道查看数据。
<span>{{ lstresult | json }}</span>
推荐阅读
- linux - Deployer SSH bash 未加载配置文件
- css - 带有引导程序的透明导航栏无法按预期工作
- javascript - 为什么在将条件放入其中时for循环会在浏览器中崩溃?
- c++ - 为什么编译器说我的整数变量没有被使用?
- python - __init__.py”,第 92 行,在
raise RuntimeError("需要 Python 3.5 或更高版本") - azure-data-explorer - 从应用服务访问 Azure 数据资源管理器
- javascript - 将 SVG 字符串转换为 base64
- node.js - 将变量与会话(快速会话)一起使用 - 登录和用户名有效,但其他无效
- reactjs - 在 React 中访问输入值
- r - 逻辑回归的预测函数返回整个数据集的结果,而不仅仅是训练数据集