Angular配置http请求
·
Angular中配置http请求服务及请求跨域_@不喝海之言@-CSDN博客_angular请求跨域
1. 在app.module.ts中引入我们http请求模块:
// 引入angular的内置请求模块
import {HttpClientModule} from '@angular/common/http'
2. 在app.module.ts的模块配置中,配置我们引入的http请求模块:
// 配置模块
imports: [
BrowserModule,
// 请求模块
HttpClientModule
]
3.1. 如果只是单单使用get请求
// 在组件中引入http请求服务---get请求
import {HttpClient} from '@angular/common/http'
3.2. 如果还要用post请求的话,还需要引入httpHeaders
// 如果是post请求,还需要引入httpHeaders
import {HttpClient,HttpHeaders} from '@angular/common/http'
然后在我们组件的ts文件的constructor构造器中声明一下即可
// 声明
constructor(public http:HttpClient) { }
更多推荐
所有评论(0)