当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Angular 从入坑到挖坑 - HTTP 请求概览

Angular 从入坑到挖坑 - HTTP 请求概览

2020年03月23日  | 移动技术网IT编程  | 我要评论

一、overview

angular 入坑记录的笔记第四篇,介绍在 angular 中如何通过 httpclient 类发起 http 请求,从而完成与后端的数据交互。

对应官方文档地址:

配套代码地址:angular-practice/src/http-guide

二、contents

  1. angular 从入坑到弃坑 - angular 使用入门
  2. angular 从入坑到挖坑 - 组件食用指南
  3. angular 从入坑到挖坑 - 表单控件概览
  4. angular 从入坑到挖坑 - http 请求概览

三、knowledge graph

思维导图

四、step by step

4.1、与后端进行数据交互

4.1.1、前置工作

在前端项目与后端进行数据交互时,绝大多数都是通过 http 协议进行的,现代浏览器支持两种方式向后端发起 http 请求:xmlhttprequest 和 fetch

在以前的项目中,通常使用 jquery 的简化版 ajax 请求向后端请求数据,归根到底最终还是通过 xmlhttprequest 与后端进行数据交互

在 angular 中, 为了简化 xmlhttprequest 的使用,框架提供了 httpclient 类来封装 http api,用来实现前端与后端的数据交互。

在使用之前,首先需要在应用的根模块中,引入 httpclientmodule 模块,并添加到 imports 数组中

import { browsermodule } from '@angular/platform-browser';
import { ngmodule } from '@angular/core';

import { approutingmodule } from './app-routing.module';
import { appcomponent } from './app.component';

// 添加对于 httpclientmodule 模块的引用
import { httpclientmodule } from '@angular/common/http';

@ngmodule({
  declarations: [
    appcomponent
  ],
  imports: [
    browsermodule,
    approutingmodule,
    httpclientmodule // 添加到根应用模块中
  ],
  providers: [],
  bootstrap: [appcomponent]
})
export class appmodule { }

在需要使用到的地方,引入 httpclient 类,然后通过依赖注入的方式注入到应用类中

在通常情况下,我们需要将与后端进行交互的行为封装成服务,在这个服务中完成对于获取到的数据的处理,之后再注入到需要使用该服务的组件中,从而确保组件中仅仅包含的是必要的业务逻辑行为

import { injectable } from '@angular/core';

// 引入 httpclient 类
import { httpclient } from '@angular/common/http';

@injectable({
  providedin: 'root'
})
export class antimotivationalquotesservicesservice {

  // 通过构造函数注入的方式依赖注入到使用的类中
  constructor(private http: httpclient) { }
}
import { component, oninit } from '@angular/core';

// 引入服务
import { antimotivationalquotesservicesservice } from './../services/anti-motivational-quotes-services.service';

@component({
  selector: 'app-anti-motivational-quotes',
  templateurl: './anti-motivational-quotes.component.html',
  styleurls: ['./anti-motivational-quotes.component.scss']
})
export class antimotivationalquotescomponent implements oninit {

  // 通过构造函数注入的方式使用服务
  constructor(private services: antimotivationalquotesservicesservice) { }

  ngoninit(): void {
  }

}
4.1.2、从服务端获取数据

这里使用到的后端接口是掘金上一位朋友开发的毒鸡汤接口(),所有权归属于

后端接口

通过使用 postman 进行接口调用可以发现,接口返回的响应信息如下

获取数据

在项目中创建一个接口,按照后端返回的数据信息进行属性的定义,用来映射请求的响应信息(angular 只能将请求响应对象转换成接口类型,不能自动转换成类实例)

ng g interface interfaces/get-quotes-response-model
export interface getquotesresponsemodel {
  /**
   * 接口响应码
   */
  code: number;
  /**
   * 响应信息
   */
  msg: string;
  /**
   * 响应数据
   */
  data: responsedata;
  /**
   * 响应时间
   */
  time: number;
}

/**
 * 接口响应的内容信息
 */
interface responsedata {
  /**
   * 毒鸡汤
   */
  content: string;
  /**
   * 热度
   */
  hots: number;
}

在服务中,引入请求响应对象的接口定义,然后设定 get 请求的响应对象为 getquotesresponsemodel,之后在使用时就可以以一种结构化数据的方式获取请求返回的数据信息

import { injectable } from '@angular/core';
import { observable } from 'rxjs';

// 引入 httpclient 类
import { httpclient } from '@angular/common/http';

// 引入接口响应类
import { getquotesresponsemodel } from '../interfaces/get-quotes-response-model';

@injectable({
  providedin: 'root'
})
export class antimotivationalquotesservicesservice {

  // 通过构造函数注入的方式依赖注入到使用的类中
  constructor(private http: httpclient) { }

  /**
   * 通过 get 请求获取毒鸡汤信息
   */
  getantimotivationalquotes(): observable<getquotesresponsemodel> {
    const url = 'https://api.tryto.cn/djt/text';
    return this.http.get<getquotesresponsemodel>(url);
  }
}

在组件中,通过调用注入的服务类完成接口数据的获取,因为是以一种结构化对象的形式获取到接口返回的数据,因此这里可以直接通过对象属性获取到指定的属性信息

import { component, oninit } from '@angular/core';

// 引入服务
import { antimotivationalquotesservicesservice } from './../services/anti-motivational-quotes-services.service';

// 引入接口响应对象
import { getquotesresponsemodel } from '../interfaces/get-quotes-response-model';

@component({
  selector: 'app-anti-motivational-quotes',
  templateurl: './anti-motivational-quotes.component.html',
  styleurls: ['./anti-motivational-quotes.component.scss']
})
export class antimotivationalquotescomponent implements oninit {

  public quoteresponse: getquotesresponsemodel;

  // 通过构造函数注入的方式使用服务
  constructor(private services: antimotivationalquotesservicesservice) { }

  ngoninit(): void {
  }

  /**
   * 获取毒鸡汤
   */
  getquotes() {
    this.services.getantimotivationalquotes().subscribe((response: getquotesresponsemodel) => {
      this.quoteresponse = response;
    });
  }
}

因为最终需要的信息是接口返回的响应信息对象中的一个属性,因此这里需要使用安全导航运算符(?)来确保模板的渲染不会因为空指针错误而中断

<p>
  <button (click)="getquotes()">获取毒鸡汤</button>
</p>
<p>
  接口返回信息: {{quoteresponse | json}}
</p>
<i>
  毒鸡汤:{{quoteresponse?.data?.content}}
</i>

请求示例

在执行服务中的方法时,有时会存在没有回调函数的情况,此时也必须执行 subscribe 方法,否则服务中的 http 请求是没有真正发起的

服务中的 getantimotivationalquotes 只能获取到接口返回的 body 里面的信息,某些情况下需要获取到完整的响应信息,此时需要通过 observe 参数来告诉 httpclient 此方法需要返回完整的响应信息

配置请求参数

import { injectable } from '@angular/core';
import { observable } from 'rxjs';

// 引入 httpclient 类
import { httpclient, httpresponse } from '@angular/common/http';

// 引入接口响应类
import { getquotesresponsemodel } from '../interfaces/get-quotes-response-model';

@injectable({
  providedin: 'root'
})
export class antimotivationalquotesservicesservice {

  // 通过构造函数注入的方式依赖注入到使用的类中
  constructor(private http: httpclient) { }

  /**
   * 获取完整的接口请求信息
   */
  getantimotivationalquotesresponse(): observable<httpresponse<getquotesresponsemodel>> {
    const url = 'https://api.tryto.cn/djt/text';
    return this.http.get<getquotesresponsemodel>(url, { observe: 'response' });
  }
}
import { httpresponse } from '@angular/common/http';
import { component, oninit } from '@angular/core';

// 引入服务
import { antimotivationalquotesservicesservice } from './../services/anti-motivational-quotes-services.service';

// 引入接口响应对象
import { getquotesresponsemodel } from '../interfaces/get-quotes-response-model';

@component({
  selector: 'app-anti-motivational-quotes',
  templateurl: './anti-motivational-quotes.component.html',
  styleurls: ['./anti-motivational-quotes.component.scss']
})
export class antimotivationalquotescomponent implements oninit {

  public quoteresponseinfo: httpresponse<getquotesresponsemodel>;

  // 通过构造函数注入的方式使用服务
  constructor(private services: antimotivationalquotesservicesservice) { }

  ngoninit(): void {
  }

  /**
   * 获取毒鸡汤接口完整的请求信息
   */
  getquotesresponse() {
    this.services.getantimotivationalquotesresponse().subscribe((response: httpresponse<getquotesresponsemodel>) => {
      this.quoteresponseinfo = response;
    });
  }
}

获取完整的请求响应信息

httpclient 默认的返回信息格式都是 json 对象,在后端接口返回的并不是 json 对象的情况下,需要手动的设置响应类型(text、blob、arraybuffer...)

import { injectable } from '@angular/core';
import { observable } from 'rxjs';

// 引入 httpclient 类
import { httpclient, httpresponse } from '@angular/common/http';

// 引入接口响应类
import { getquotesresponsemodel } from '../interfaces/get-quotes-response-model';

@injectable({
  providedin: 'root'
})
export class antimotivationalquotesservicesservice {

  // 通过构造函数注入的方式依赖注入到使用的类中
  constructor(private http: httpclient) { }
  
  /**
   * 获取响应类型非 json 对象的信息
   */
  getyuitersitemap(): observable<string> {
    const url = 'https://yuiter.com/sitemap.xml';
    return this.http.get(url, { responsetype: 'text' });
  }
}

获取响应对象不是 json 对象的信息

4.1.3、提交数据到服务端

在同后端接口进行交互时,获取数据一般用的是 get 请求,而当进行数据新增、更新、删除时则会使用 post、put、delete 这三个 http 谓词

在毒鸡汤这个接口中,可以使用 post 方式调用 进行毒鸡汤的提交

提交毒鸡汤

根据 postman 的调用示例,在服务中定义一个方法用来提交毒鸡汤信息,这里的 setquotesresponsemodel 为接口返回的响应对象

import { injectable } from '@angular/core';
import { observable } from 'rxjs';

// 引入 httpclient 类
import { httpclient, httpresponse } from '@angular/common/http';

// 引入接口响应类
import { setquotesresponsemodel } from '../interfaces/set-quotes-response-model';

@injectable({
  providedin: 'root'
})
export class antimotivationalquotesservicesservice {

  // 通过构造函数注入的方式依赖注入到使用的类中
  constructor(private http: httpclient) { }

  /**
   * 提交毒鸡汤信息
   * @param content 毒鸡汤
   */
  submitantimotivationalquote(content: string): observable<setquotesresponsemodel> {
    const url = 'https://api.tryto.cn/djt/submit';
    return this.http.post<setquotesresponsemodel>(url, {
      content
    });
  }
}

使用 post 方法向服务器提交数据

因为这里是以默认的表单提交的方式进行的数据提交,当后端需要修改请求的 body 格式时,则需要我们修改请求的 mime 类型

当需要更改请求的 mime 类型或是需要添加授权访问的 token 信息这一类的操作时,需要在使用 httpclient 提供的请求方法时添加上 http 请求头配置信息

import { injectable } from '@angular/core';
import { observable } from 'rxjs';

// 引入 httpclient 类
import { httpclient, httpresponse, httpheaders } from '@angular/common/http';

@injectable({
  providedin: 'root'
})
export class antimotivationalquotesservicesservice {

  // 通过构造函数注入的方式依赖注入到使用的类中
  constructor(private http: httpclient) { }

  public httpoptions = {
    headers: new httpheaders({
      'content-type': 'application/json',
      'authorization': 'token'
    })
  };

  /**
   * 修改请求头信息
   */
  submitwithoptions() {
    const url = '';
    return this.http.post(url, {
      data: ''
    }, this.httpoptions);
  }
}

4.2、捕获错误信息

4.2.1、获取错误信息

在涉及到前后端交互的过程中,不可避免会出现各种状况,在出现错误时,可以在 subscribe 方法中,添加第二个回调方法来获取错误信息

getquotes() {
    this.services.getantimotivationalquotes().subscribe((response: getquotesresponsemodel) => {
      this.quoteresponse = response;
    }, error => {
      console.error(error);
    });
}

获取错误信息

在处理错误信息的回调方法中,方法返回了一个 httperrorresponse 对象来描述错误信息

因为这里的错误更多是服务在与后端进行通信产生的错误,因此对于错误信息的捕获和处理更应该放到服务中进行,而在组件处仅显示错误提示

在服务中定义一个错误处理器,用来处理与后端请求中发生的错误

import { injectable } from '@angular/core';
import { observable, throwerror } from 'rxjs';
import { catcherror, retry } from 'rxjs/operators';

// 引入 httpclient 类
import { httpclient, httpresponse, httpheaders, httperrorresponse } from '@angular/common/http';

@injectable({
  providedin: 'root'
})
export class antimotivationalquotesservicesservice {

  // 通过构造函数注入的方式依赖注入到使用的类中
  constructor(private http: httpclient) { }

  /**
   * 通过 get 请求获取毒鸡汤信息
   */
  getantimotivationalquotes(): observable<getquotesresponsemodel> {
    const url = 'https://api.tryto.cn/djt/text32';
    return this.http.get<getquotesresponsemodel>(url)
      .pipe(
        catcherror(this.handleerror)
      );
  }

  /**
   * 错误信息捕获处理
   * @param error 错误信息
   */
  private handleerror(error: httperrorresponse) {

    if (error.error instanceof errorevent) {
      // 客户端本身引起的错误信息
      console.error(`客户端错误:${error.error.message}`);
    } else {
      // 服务端返回的错误信息
      console.error(`服务端错误:http 状态码:${error.status} \n\r 错误信息:${json.stringify(error.error)}`);
    }

    // 反馈给用户的错误信息(用于组件中使用 error 回调时的错误提示)
    return throwerror('不好的事情发生了,毕竟我们都有不顺利的时候。。。');
  }
}

当请求发生错误时,通过在 httpclient 方法返回的 observable 对象中使用 pipe 管道将错误传递给自定义的错误处理器,从而完成捕获错误信息的后续操作

通过自定义的错误处理器完成错误信息的捕获

4.2.2、请求重试

某些情况下存在因为特殊原因导致短时间的请求失败,这时可以在 pipe 管道中,当请求失败后,使用 retry 方法进行多次的请求重试,在进行了多次重试后还是无法进行数据通信后,则进行错误捕获

getantimotivationalquotes(): observable<getquotesresponsemodel> {
    const url = 'https://api.tryto.cn/djt/text32';
    return this.http.get<getquotesresponsemodel>(url)
      .pipe(
        retry(3), // 重试三次
        catcherror(this.handleerror) // 捕获错误信息
      );
}

请求失败后进行重试

4.3、请求和响应拦截

在向服务器发起请求时,一般是需要我们在请求头中添加上授权的 token 信息,与其当后端接口返回我们无权访问时再来处理,是不是可以在发起请求前去进行拦截判断,如果不包含 token 信息,则将允许访问的 token 信息添加到请求中

同样的,当已经定义好后端返回什么信息代表请求出错 or 直接根据后端返回的请求状态码判断请求出错时,完全可以通过对接口返回的响应进行拦截,直接拦截掉请求出错的情况,从而不需要在后续的业务逻辑代码中再进行判断请求是否成功

4.3.1、自定义拦截器

在 angular 中可以新建一个继承于 httpinterceptor 接口的拦截器类,通过实现 intercept 方法来对请求进行拦截处理

与 core 中的中间件相似,我们可以在请求中添加多个的拦截器,构成一个拦截器链。当一个拦截器已经处理完成时,需要通过 next 对象将 http 请求传递到下一个拦截器,否则,整个请求将会中断。如果当前的拦截器已经是整个拦截器链的最后一个,则会将请求发送到后端接口

import { httpinterceptor, httprequest, httphandler, httpevent, httpresponse } from '@angular/common/http';
import { observable } from 'rxjs/internal/observable';
import { injectable } from '@angular/core';
import { tap, finalize } from 'rxjs/operators';

/**
 * 通过添加 injectable 特性,表明可以通过依赖注入的方式进行创建
 */
@injectable()
export class logginginterceptor implements httpinterceptor {
  /**
   * 请求拦截
   * @param req http 请求
   * @param next 下一个拦截器
   */
  intercept(req: httprequest<any>, next: httphandler): observable<httpevent<any>> {

    // 开始时间
    const started = date.now();

    let msg: string;

    // 将 http 请求信息传递给下一个拦截器
    return next.handle(req)
      .pipe(
        tap(
          // 捕获当前请求是否成功 or 失败

          // 1、通过判断响应的类型是否为 httpresponse 来判断请求是否成功
          event => msg = event instanceof httpresponse ? '请求成功' : '请求失败',

          // 2、如果存在了 error 回调,则请求失败
          error => msg = '请求失败'
        ), finalize(() => {
          const elapsed = date.now() - started;
          console.log(`请求方式:${req.method} 请求地址:${req.urlwithparams} 响应耗时:${elapsed} ms 请求结果:${msg}`);
        }));
  }
}

当定义好拦截器后,与其它的自定义服务一样,我们需要添加到根模块的 providers 中,因为可能会存在定义多个拦截器的情况,这里可以通过定义一个 typescript 文件用来导出我们需要添加的拦截器信息

因为会存在定义多个拦截器的情况,所以这里需要指定 multi 属性为 true

import { http_interceptors } from '@angular/common/http';

// 需要添加的拦截器
import { logginginterceptor } from './logging-interceptor';

// 返回的拦截器数组
export const httpinterceptorproviders = [
  { provide: http_interceptors, useclass: logginginterceptor, multi: true }
];

由于拦截器具有将发送到服务端的 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 { httpinterceptorproviders } from './http-interceptors/http-interceptor-providers';

@ngmodule({
  declarations: [
    appcomponent,
    antimotivationalquotescomponent
  ],
  imports: [
    browsermodule,
    approutingmodule,
    formsmodule,
    httpclientmodule // 添加到根应用模块中
  ],
  providers: [
    httpinterceptorproviders
  ],
  bootstrap: [appcomponent]
})
export class appmodule { }

添加拦截器

4.3.2、修改请求信息

由于一个请求可能会存在重试发起的情况,为了确保多次发起请求时的请求信息的不变性,对于 httprequest 和 httpresponse 我们是不可以修改原始的对象属性值的

当我们需要对请求进行修改时,例如在请求的 header 中添加上 token 信息,此时我们需要先克隆一个原始的请求对象,在这个克隆后的请求上进行操作,最终将这个克隆后的请求传递给下一个拦截器

import { httpinterceptor, httprequest, httphandler, httpevent, httpresponse } from '@angular/common/http';
import { observable } from 'rxjs/internal/observable';
import { injectable } from '@angular/core';
import { tap, finalize } from 'rxjs/operators';

/**
 * 通过添加 injectable 特性,表明可以通过依赖注入的方式进行创建
 */
@injectable()
export class logginginterceptor implements httpinterceptor {
  /**
   * 请求拦截
   * @param req http 请求
   * @param next 下一个拦截器
   */
  intercept(req: httprequest<any>, next: httphandler): observable<httpevent<any>> {

    // 开始时间
    const started = date.now();

    let msg: string;

    // 打印原始的请求信息
    console.log(`原始的请求信息:${json.stringify(req.headers)}`);

    // 获取请求中的 token 信息
    const token = req.headers.get('authorization') || '';

    // 克隆请求信息
    const authreq = req.clone({
      headers: token === '' ? req.headers.set('authorization', '123456') : req.headers
    });

    // 打印修改后的请求信息
    console.log(`克隆后的请求信息:${json.stringify(authreq.headers)}`);

    // 将克隆后的 http 请求信息传递给下一个拦截器
    return next.handle(authreq)
      .pipe(
        tap(
          // 捕获当前请求是否成功 or 失败

          // 1、通过判断响应的类型是否为 httpresponse 来判断请求是否成功
          event => msg = event instanceof httpresponse ? '请求成功' : '请求失败',

          // 2、如果存在了 error 回调,则请求失败
          error => msg = '请求失败'
        ), finalize(() => {
          const elapsed = date.now() - started;
          console.log(`请求方式:${req.method} 请求地址:${req.urlwithparams} 响应耗时:${elapsed} ms 请求结果:${msg}`);
        }));
  }
}

克隆请求信息

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网