html - 尝试在 Angular 5 中使用 get 调用其余 api
问题描述
import { Component, OnInit } from '@angular/core';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
configUrl = '/config.json';
getConfig() {
return this.http.get(this.configUrl);
}
@Component({
selector: 'app-rest',
templateUrl: './rest.component.html',
styleUrls: ['./rest.component.css']
})
@Injectable()
export class RestComponent {
constructor(private http: HttpClient) { }
}
我已经在config.json
{
"heroesUrl": "http://xx.xxx.xx.xxx:3001/getAllData"
}
我正在使用另一个文件中的 herosUrl 来调用其余 api,并且我已将文件链接到我的主文件夹中,但是我收到错误请帮助我以角度调用其余 api
2)在conig url中,我传递带有config.json扩展名的url
解决方案
将 http.get 放在您的组件代码中,
export class RestComponent {
constructor(private http: HttpClient) { }
getConfig() {
return this.http.get(this.configUrl);
}
}
推荐阅读
- apache-kafka - 是否可以跨 kafka 集群修复参数 min.insync.replicas?
- google-cloud-platform - 谷歌播放服务上的无限加载屏幕
- c# - c# Double.MinValue 和 Double.MaxValue 之间的随机数
- javascript - Three.js从点列表中绘制椭圆
- mule - 如何从有效载荷中的字符串值中获取值,
- android - 使用 Tensorflowlite 的 Android 应用程序中的 ML 项目
- .net-core - IIS下如何为不同环境配置dotnet core web app
- python - Mac 上的 Python os.cpu_count() 返回错误的内核数
- css - 在 iOS 中的 Safari 上向下滚动时,当在容器上设置高度时,页面很容易横向滚动
- c# - 是否可以使用 2 个单独的密码创建 ASP.NET 用户?