Я изучаю Angular و и хочу добавить WebApi, созданный в Asp. Net Core به, в мой проект, но у меня возникают проблемы при печати информации.
Шаг 1: приложение .module.ts
import {HttpClientModule} from '@angular/common/http'
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
HttpClientModule],
Шаг 2: person-servise.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { IPerson } from 'src/IPerson';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class PersonServiseService {
constructor(private http:HttpClient) { }
private Url:string='https://localhost:44365/api/todo';
getDate():Observable<IPerson[]>
{
return this.http.get<IPerson[]>(this.Url);
}
}
Шаг 3: Создать IPerson в Sr c
export interface IPerson
{
Id:number,
FName:string,
LName:string,
Age:number
}
Шаг 4: app.component.ts
import { Component, OnInit } from '@angular/core';
import { PersonServiseService } from './person-servise.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'ProjectTwo';
constructor(private _persen:PersonServiseService){ }
ngOnInit(){
this._persen.getDate().subscribe(data=> this.persen=data);
}
persen:any[]=[];
}
Шаг 5: app.component. html
<table>
<thead>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</thead>
<tbody *ngFor="let item of persen">
<td>{{item.Id}}</td>
<td>{{item.FName}}</td>
<td>{{item.LName}}</td>
</tbody>
</table>
Я протестировал WebApi, созданный с использованием Asp. Net Core Post с программным обеспечением Postman, и операции Get, Post, Put, Delete работают нормально, но не отображаются в выводе Angular.
вложение:
public class TodoItem
{
public long Id { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public int Age { get; set; }
}