get
在app.module.ts 引入模块import {HttpClientModule } from '@angular/common/http'
在app.module.ts引用
imports: [BrowserModule, AppRoutingModule, FormsModule, `HttpClientModule`],
在需要请求的页面引入 ponents.ts
import {HttpClient } from '@angular/common/http';
在需要请求的页面构造器声明HttpClient
constructor(public storage: StorageService, public http: HttpClient) {}
然后就可以直接用方法了
getHttp() {let api = '/api/productlist';this.http.get(api).subscribe((response) => {console.log(response);});}
post
import {HttpClient, HttpHeaders } from '@angular/common/http';doLogin() {const httpOptions = {headers: new HttpHeaders({'Content-Type': 'application/json' }),};let api = '127.0.0.1:3000/dologin';this.http.post(api, {username: '张三' }, httpOptions).subscribe((response) => {console.log(response);});}
如果觉得《记录angular自带方法发送 get post请求》对你有帮助,请点赞、收藏,并留下你的观点哦!