首页 > 解决方案 > 尝试在 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

标签: htmlangularrestapi

解决方案


将 http.get 放在您的组件代码中,

export class RestComponent  {
  constructor(private http: HttpClient) { }
  getConfig() {
   return this.http.get(this.configUrl);
  }
} 

推荐阅读