我正在嘗試創建一個 Employee 型別的變數,但它顯示錯誤“Employee”僅指一種型別,但在此處用作值。ts(2693)。
我的 show-api.ts 代碼:-
import { Component, OnInit } from '@angular/core';
import {HttpClient} from '@angular/common/http';
interface Employee {
Id: String;
EmployeeName: String;
StartTimeUtc: String;
EndTimeUtc: String;
EntryNotes: String;
DeletedOn: String;
}
@Component({
selector: 'app-show-api',
templateUrl: './show-api.component.html',
styleUrls: ['./show-api.component.css']
})
export class ShowApiComponent implements OnInit {
li:any;
lis: any=[];
constructor(private http : HttpClient){
}
ngOnInit(): void {
this.http.get('Api of my JSON file')
.subscribe(Response => {
if(Response){
hideloader();
}
**employee: Employee[]=Response**;
});
function hideloader(){
document.getElementById('loading')!.style.display = 'none';}
}}
來自 API 的資料
[
{"Id": "aa",
"EmployeeName": "bb",
"StarTimeUtc": "cc",
"EndTimeUtc": "dd",
"EntryNotes": "ee",
"DeletedOn": null },
{"Id": "ff",
"EmployeeName": "gg",
"StarTimeUtc": "hh",
"EndTimeUtc": "ii",
"EntryNotes": "jj",
"DeletedOn": null },
]
此外,如果這無法解決,請告訴我另一種從 JSON 檔案獲取資料的方法。
uj5u.com熱心網友回復:
問題是您沒有以正確的方式宣告變數,也沒有映射Response到您的界面。
ngOnInit(): void {
this.http.get('Api of my JSON file')
.subscribe(Response => {
if(Response){
hideloader();
}
//oldCode -> **employee: Employee[]=Response**;
/* New code */ const employee: Employee[] = Response as Employee[];
});
function hideloader(){
document.getElementById('loading')!.style.display = 'none';}
}}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/436128.html
