c# - 如何在角度服务中调用 web api
问题描述
我是角度的新手。有人可以帮我知道如何用角码调用我的 api 吗?
我的 We API 是 -
[HttpGet,Route("api/customer/getCustomer/{name}")]
public async Task<IHttpActionResult> getCustomer([FromUri] string name) {
...}
在anular中,我想像下面这样打电话 -
customer.component.ts -
name = "test";
getCustomer(){
this.custService.getCustomer(this.name).subscribe((data) => {
this.cust = JSON.parse(data.toString());
});
}
custService 代码 -
getCustomer(name){
return this.http.get('https://localhost:44400/api/customer/getCustomer/', +name)
}
解决方案
客户.component.ts
export class CustomerComponent implements OnInit {
name: string;
cust: any;
constructor(private custService: CustService){
}
ngOnInit(){
this.name = "test";
this.getCustomer();
}
getCustomer(){
this.custService.getCustomer(this.name).subscribe((data) => {
this.cust = data;
},error => {
console.log(error);
}
);
}
推荐阅读
- python - Python 中的 ASN1 和规范八位字节编码规则 (COER)
- javascript - 我无法启用实验性 JavaScript,
- c# - 什么可能导致“这可能表明 EF Core 中存在错误或限制”。linq 表达式.net core 3.0 中的错误?
- asp-classic - Request.ServerVariables("..") 在分配给变量时返回 undefined 但其值可以通过 Response.Write("..") 显示
- java - HIbernate 查询参数绑定抛出 QuerySyntaxException
- r - 接收错误“美学必须是长度1或与数据相同(9420):颜色”在R
- google-analytics - Google Analytics:哪些事件将用户引导至最终事件
- domain-driven-design - DDD 实体工厂职责
- c# - 文件上传没有图像路径可以保存到服务器
- javascript - 在 electrons-js 应用程序中使用“Cast Service”