Angular中http请求模块的使用方法
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍了Angular中http请求模块的使用方法,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
首先模块引入
import { BrowserModule } from '@angular/platform-browser';import { NgModule } from '@angular/core';import {FormsModule} from '@angular/forms';import { AppRoutingModule } from './app-routing.module';import { AppComponent } from './app.component';import {HttpClientModule,HttpClientJsonpModule} from '@angular/common/http'@NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule, FormsModule, HttpClientModule, HttpClientJsonpModule ], providers: [], bootstrap: [AppComponent]})export class AppModule { }
组件中使用
import { Component, OnInit } from '@angular/core';import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';import Qs from 'qs';@Component({ selector: 'app-http', templateUrl: './http.component.html', styleUrls: ['./http.component.less']})export class HttpComponent implements OnInit { constructor(public http: HttpClient) { } ngOnInit(): void { this.getPostData(); //post this.getTestData(); //get this.getJsonpData() //jsonp } getPostData() { this.http.post('http://localhost:3000/api/info', { name: 'laney' }, { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }).subscribe((res) => { console.log(res); }) } getTestData() { var obj1 = { name: 'alice', age: '20' } var params = Qs.stringify(obj1); console.log(params) //第一种方式:字符串拼接 this.http.get('http://localhost:3000/api/school?' + params).subscribe((res) => { console.log(res); }) //第二种方式:HttpParams var oarma = new HttpParams({ fromString: params }); this.http.get('http://localhost:3000/api/school?', { params: oarma }).subscribe((res) => { console.log(res); }) } getJsonpData() { this.http.jsonp('http://localhost:3000/getscript', 'callback').subscribe((res) => { console.log(res); }) }
http封装
import { Injectable } from '@angular/core';import {HttpClient,HttpHeaders,HttpParams} from '@angular/common/http';import Qs from 'qs';import { environment } from '../../environments/environment';console.log(environment.baseURL);@Injectable({ providedIn: 'root'})export class RxjsService { constructor(public http:HttpClient) { } postFun(url,data){ return this.http.post(environment.baseURL+url,data,{ headers:new HttpHeaders({ 'Content-Type':'application/json' }) }) } getFun(url,data){ var params = Qs.stringify(data); return this.http.get(environment.baseURL+url+'?'+params) }}
使用
import { Component, OnInit } from '@angular/core';import {RxjsService} from '../../services/rxjs.service';@Component({ selector: 'app-rxjs', templateUrl: './rxjs.component.html', styleUrls: ['./rxjs.component.less']})export class RxjsComponent implements OnInit { constructor(public rxjs:RxjsService) { } ngOnInit(): void { } getRXJS(){ this.rxjs.getFun('/api/school',{ name:'alice' }).subscribe((res)=>{ console.log(res); }) } postRXJS(){ this.rxjs.postFun('/api/info',{ name:'alice' }).subscribe((res)=>{ console.log(res); }) }}
感谢你能够认真阅读完这篇文章,希望小编分享的“Angular中http请求模块的使用方法”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网行业资讯频道,更多相关知识等着你来学习!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341