我正在try 创建一个以.Net Core为后端的棱角仪表板.我创建了两个组件Component1和Component2.当我try 通过API获取Component1中的数据时,它工作得很好,但当我try 对Component2执行相同的操作时,数据在前端不可见,但在控制台上显示.我不确定这里到底是什么问题.我正在为这个项目使用material

labour-charge-type.service.ts

 import { Injectable } from '@angular/core';
import { LabourChargeType } from '../models/labour-charge-type';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';

@Injectable({
  providedIn: 'root'
})
export class LabourChargeTypeService {
  private url="LabourChargeType";

  constructor(private http:HttpClient ) { }
  public getLabourChargeType():Observable<LabourChargeType[]>{
    return this.http.get<LabourChargeType[]>(`${environment.apiUrl}/${this.url}`);
  }
}

Labour-charge-type.component.html

<div class="maindiv">
    <div class="row ">
        <div class="col-md-12">
            <h3>Labour Charge Type</h3>
        </div>
    </div>
    <div>
        <hr>
    </div>
    <div class="table-container">
        <table class="custom-table">
            <thead>
                <tr>
                    <th class="custom-header">Name</th>
                    <th class="custom-header">Code</th>
                    <th class="custom-header">Status</th>
                    <th class="custom-header">Actions</th>
                </tr>
            </thead>
            <tbody>
                <tr class="table-row" *ngFor="let cs of labourchargetype">
                    <td class="table-data">{{ cs.LabourChargeTypeName }}</td>
                    <td class="table-data">{{ cs.LabourChargeTypeCode }}</td>
                    <td class="table-data">{{ cs.LabourChargeTypestatus }}</td>
                    <td class="table-data">
                        <button class="edit-button">Edit</button>
                        <button class="delete-button">Delete</button>
                    </td>
                </tr>
            </tbody>
        </table>        
    </div>
    </div>

Labour-charge-type.component.ts

import { Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { Router } from '@angular/router';
import { LabourChargeType } from 'src/app/models/labour-charge-type';
import { LabourChargeTypeService } from 'src/app/services/labour-charge-type.service';

@Component({
  selector: 'app-labour-charge-type',
  templateUrl: './Labour-charge-type.component.html',
  styleUrls: ['./labour-charge-type.component.scss']
})
export class LabourChargeTypeComponent implements OnInit {
  labourchargetype: LabourChargeType[] = [];
  constructor(private router: Router, private dialog: MatDialog, private labourChargeTypeService: LabourChargeTypeService) {}

  ngOnInit(): void {

    this.labourChargeTypeService.getLabourChargeType()
    .subscribe(
      (labourcharget: LabourChargeType[]) => {
        console.log('API Response:', labourcharget);
        this.labourchargetype = labourcharget;
      },
      error => {
        console.error('Error fetching data:', error);
      }
    );
   }
  }
}

数据将显示在控制台中

enter image description here

推荐答案

cs.LabourChargeTypeName更改为cs.labourChargeTypeName,如控制台日志(log)中所示

Typescript相关问答推荐

Tailwind CSS样式不在Svelte应用程序中呈现

如何根据数据类型动态注入组件?

TypeScript泛型参数抛出错误—?&#"' 39;预期"

typescript抱怨值可以是未定义的,尽管类型没有定义

Angular中的其他服务仅获取默认值

Redux—Toolkit查询仅在不为空的情况下添加参数

contextPaneTitleText类型的参数不能分配给remoteconfig类型的关键参数'""'''

ANGLE找不到辅助‘路由出口’的路由路径

返回具有递归属性的泛型类型的泛型函数

在包含泛型的类型脚本中引用递归类型A<;-&B

路由链接始终返回到

Angular 17子路由

为什么Typescript不能推断类型?

Typescript 中相同类型的不同结果

在TypeScript中扩展重载方法时出现问题

使用强类型元组实现[Symbol.iterator]的类型脚本?

为什么类型脚本使用接口来声明函数?他的目的是什么.

我可以将一个类型数组映射到TypeScrip中的另一个类型数组吗?

记录的子类型<;字符串,X>;没有索引签名

元素隐式具有any类型,因为string类型的表达式不能用于索引类型{Categories: Element;管理员:元素; }'