浅谈Angular中http请求模块的用法


本文摘自PHP中文网,作者青灯夜游,侵删。

本篇文章给大家介绍一下Angular中http请求模块的使用。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

相关推荐:《angular教程》

首先模块引入

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

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 { }

组件中使用

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

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封装

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

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)

  }

}

使用

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

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请求模块的用法的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

AngularAngularjs的简单比较

深入了解Angular中的表单

Angular8如何封装http服务

浅谈Angular项目中如何引入第三方ui库(Angular material)

详解Angular中jsencrypt插件的使用方法

Angular入门学习之环境和项目的搭建

16个值得收藏的Angular ui框架分享

Angular如何创建服务?5种方式了解一下!

浅谈Angular如何编译打包?如何使用docker发布?

聊聊Angular项目中将 .css 文件修改为 .scss 文件的方法

更多相关阅读请进入《Angular》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...