angular - 如何使用角度创建获取请求
问题描述
我正在尝试创建一个以角度获取请求的服务。我收到此错误: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"];
}
}
解决方案
您正在导入HttpClient
但随后Http
在构造函数中使用(未引用任何内容)。改为使用private http: HttpClient
。
推荐阅读
- java - 使用打开的 localhost 连接到 Java 服务器不起作用
- spring-boot - @Timed 注释不适用于请求范围的 bean?
- php - PHP 检查 assoc 数组的键,如果存在,将值提高 1
- wordpress-gutenberg - Wordpress Gutenberg - 嵌套子元素
- c++ - 这是什么意思:“成员引用类型'Human *'是一个指针;你的意思是使用'->'吗?”
- html - 如果有查询字符串,锚链接会在新窗口中打开,这可以防止吗?
- node.js - 直接使用节点模块和创建节点模块的对象然后使用它有什么区别?
- ios - 使用 Cocoapods 一个针对多个平台
- c++ - 编写一个函数来检查一个数字是否是一个带有递归函数的正方形
- api - Vsphere REST API - 如何在 JSON 响应中指定字段