美文网首页技术
Angular7 封装http请求

Angular7 封装http请求

作者: JADDC | 来源:发表于2018-11-30 21:40 被阅读0次

version 1 self-http.service.ts

  import { Injectable } from '@angular/core';
  import { HttpClient, HttpParams } from '@angular/common/http';


  @Injectable()
  export class SelfHttpService {
    public restServer;
    public http;

    constructor(Http: HttpClient) {
      this.http = Http;
      this.restServer = 'http://127.0.0.0/';
    }

    public get(url, params?: Object, cb?: Function) {
      let httpParams = new HttpParams();
      console.log('get开始请求');
      const vm = this;
      if (params) {
        for (const key in params) {
          if (params[key] === false || params[key]) {
            httpParams = httpParams.set(key, params[key]);
          }
        }
      }
      vm.http.get(vm.restServer + url, { params: httpParams })
        .subscribe(data => {
          console.log('get请求结束', data);
          cb(data);
        });
    }

    public post(url, data?: Object, cb?: Function, options?: Object) {
      console.log('post开始请求');
      const vm = this;
      vm.http.post(vm.restServer + url, data, options)
        .subscribe(res => {
          console.log('post请求结束', res);
          cb(res);
        });
    }

    public put(url, data?: Object, cb?: Function, options?: Object) {
      console.log('put开始请求');
      const vm = this;
      vm.http.put(vm.restServer + url, data, options)
        .subscribe(res => {
          console.log('put请求结束', res);
          cb(res);
        });
    }

    public delete(url, params?: Object, cb?: Function) {
      let httpParams = new HttpParams();
      console.log('delete开始请求');
      const vm = this;
      if (params) {
        for (const key in params) {
          if (params[key]) {
            httpParams = httpParams.set(key, params[key]);
          }
        }
      }
      vm.http.delete(vm.restServer + url, { params: httpParams })
        .subscribe(data => {
          console.log('delete请求结束', data);
          cb(data);
        });
    }
  }

version 2 self-http.service.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class SelfHttpService {

  restServer: string; // 默认为public
  http: any;

  constructor(Http: HttpClient) {
  this.http = Http;
  this.restServer = 'http://localhost';
}
public get(url: string, params?: Object, cb?: Function, options?: Object) {
let httpParams = new HttpParams();
console.log('get:start');

if (params) {
  for (const key in params) {
    if (params[key] === false || params[key]) {
      httpParams = httpParams.set(key, params[key]);
    }
  }
}


this.http.get(url, { headers: options, params: httpParams })
  .subscribe((res: any) => {
    console.log('get:end', res);
    cb(res);
  });
}

public post(url: string, data?: Object, cb?: Function, options?: Object) {
console.log('post:start');

this.http.post(url, data, options)
  .subscribe((res: any) => {
    console.log('post:end', res);
    cb(res);
  });
}

public put(url: string, data?: Object, cb?: Function, options?: Object) {
console.log('put:start');

this.http.put(url, data, options)
  .subscribe((res: any) => {
    console.log('put:end', res);
    cb(res);
  });
}

public delete(url: string, params?: Object, cb?: Function, options?: Object) {
let httpParams = new HttpParams();
console.log('delete:start');
if (params) {
  for (const key in params) {
    if (params[key]) {
      httpParams = httpParams.set(key, params[key]);
    }
  }
}
this.http.delete(url, { headers: options, params: httpParams })
  .subscribe((res: any) => {
    console.log('delete:end', res);
    cb(res);
  });
}
}

使用方法

const httpOptions = {
 headers: new HttpHeaders({
    'Content-Type': 'application/json'
 })
};


this.http.get('/api/******', keyword, res => {

  console.log('data', res);
}, httpOptions.headers.append('Authorization', this.token));

相关文章

  • Angular7 封装http请求

    version 1 self-http.service.ts version 2 self-http.servic...

  • springcloud ribbon 的简单使用

    RestTemplate 对http请求通信的封装,封装了http请求,方便的请求http接口。 Ribbon r...

  • 封装HTTP请求

    每次访问网址,需要配置网络连接的很多属性(e.i. 请求方法、连接超时时间、读取资源超时时间等),很麻烦,所以这里...

  • 封装http请求

    import 'package:dio/dio.dart'; import 'dart:async'; impor...

  • HTTP 请求封装

    前言 我们在做项目的时候经常需要用到HTTP请求,但是org.apache.http.client不满足HTTPS...

  • JavaEE-HttpServletRequest总结

    HttpServletRequest: 封装了Http请求内容(请求行, 请求头, 请求体) 1.HTTP请求行和...

  • Koa(五、源码浅析)

    基础http请求 针对http基础请求简单封装 效果上面两者相同,下面继续封装 js的getter和setter ...

  • curl发送请求方法封装request

    //使用url封装请求方法 //封装可以请求http和https //可以发送get和post的请求方式 func...

  • TCP/HTTP/Socket

    HTTP和Scoket通信的区别。 http是客户端用http协议进行请求,发送请求时候需要封装http请求头,并...

  • NO.39 WebServer代码实现

    1)先定义HTTP协议中相关信息的类 2)封装Http请求相关内容: 3)封装Http响应 4)处理客户端请求 5...

网友评论

    本文标题:Angular7 封装http请求

    本文链接:https://www.haomeiwen.com/subject/lbtuqqtx.html