首页 > 解决方案 > 如何使用角度创建获取请求

问题描述

我正在尝试创建一个以角度获取请求的服务。我收到此错误:Http 不存在。

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class CoursesService {

    constructor(private http: Http) {

        this.http.get('https://jsonplaceholder.typicode.com/users');
    }

    getCourses() {

        //return this.http.get('https://jsonplaceholder.typicode.com/users');
        return ["math", "chemistry", "physics"];

    }

}

在此处输入图像描述

标签: angular

解决方案


您正在导入HttpClient但随后Http在构造函数中使用(未引用任何内容)。改为使用private http: HttpClient


推荐阅读