gpt4 book ai didi

angular - 'Employee' 仅指类型,但在此处用作值。ts(2693)

转载 作者:行者123 更新时间:2023-12-05 05:45:34 25 4
gpt4 key购买 nike

我正在尝试创建一个 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 文件获取数据的方法。

最佳答案

问题是您没有以正确的方式声明变量,也没有将 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';}
}}

关于angular - 'Employee' 仅指类型,但在此处用作值。ts(2693),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71323917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com