尝试使用Angular 5中的get调用rest api

时间:2018-06-30 07:10:26

标签: html angular rest 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"  
}                                            

我正在使用另一个文件中的heroesUrl调用其余的api,并且我已将文件链接到主文件夹中,但是我遇到了错误,请帮我帮忙,以角方式调用其余的api

1 个答案:

答案 0 :(得分:0)

将http.get放入组件代码中,

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