c# - 在我的 Angular 11 项目中无法从 api 获取数据
问题描述
我使用 .NET Core Web API 开发了一个 Web API,然后尝试从它获取数据到我的 Angular11。
这些是 shared.service.ts 的代码
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { observable, Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class SharedService {
readonly APIUrl = "http://localhost:5000/api";
readonly PhotoUrl = "http://localhost:5000/photos";
constructor(private http: HttpClient) { }
getSecList(): Observable<any[]> {
return this.http.get<any>(this.APIUrl + '/section');
}}
显示-sec.component.ts
import { Component, OnInit } from '@angular/core';
import { SharedService } from './../../shared.service';
@Component({
selector: 'app-show-sec',
templateUrl: './show-sec.component.html',
styleUrls: ['./show-sec.component.css']
})
export class ShowSecComponent implements OnInit {
constructor(private service: SharedService) { }
SectionList: any = [];
ngOnInit(): void {
this.refreshSecList();
}
refreshSecList() {
this.service.getSecList().subscribe(data => {
this.SectionList = data;
console.log(this.SectionList);
});
}
}
解决方案
推荐阅读
- angular - 将 matInput 的值绑定到其创建的数组
- java - 如何存储列表中的值
- vue.js - Vue路由器从URL中删除参数
- java - Primefaces Datatable pdf 导出自定义列宽
- angular - 安全退订可选控制器订阅
- neo4j - 在处理大量数据时修改查询以获得更好的性能
- bluetooth-lowenergy - 禁用 LE Set Scan Enable 命令的“过滤重复”设置
- python-3.x - 无法使用 pd.to_datetime 解析时间字符串
- mongodb - MongoDB dataSize 命令错误“找不到包含键模式的有效索引”
- sql - 将隐式连接转换为显式连接