在前端项目中,往往在请求服务端的 api 的时候,我们需要做一些跨域、统一前缀、添加 cookie 等 header 信息的处理,这时候,在 angular 中,就用到了 httpInterceptor
,下面介绍如何在 angular 中使用 httpInterceptor。
在angular
项目根目录下创建service
1 | ng g service httpInterceptor |
httpInterceptorService 其实就是一个实现了 angular 中 HttpInterceptor 接口的 service,然后实现接口中的 intercept 方法。
1 | import { Injectable } from '@angular/core'; |
引入@angular/common/http 模块
1 | import { |
引入 rxjs 操作符
1 | import { Observable } from 'rxjs'; |
finally code
1 | export class HttpInterceptorService implements HttpInterceptor { |
- 本文链接: https:https://whyour.cn/post/httpInterceptor.html
- 最后更新于:
- 版权声明: 本博客所有文章除特别声明外,均采用 署名 4.0 国际(CC BY 4.0) 许可协议。转载请注明出处!
舟夜书所见
用angular/cli和typescript开发npm模块