@angular/cli项目构建--interceptor
JWTInterceptor
import {Injectable} from '@angular/core';
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class JWTInterceptor implements HttpInterceptor {
constructor() {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
req = req.clone({
setHeaders: {Authorization: 'Token Token_admin_id'}
});
return next.handle(req);
}
}
ResponseInterceptor
import {Injectable} from '@angular/core';
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class ResponseInterceptor implements HttpInterceptor {
constructor() {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).map(event => {
if (event instanceof HttpResponse) {
switch (event.status) {
case 401:
// expired jwt go to login
break;
case 404:
// not found
break;
case 403:
// forbidden
break;
case 500:
// service inner error
break;
default:
console.log('data return successfully!');
}
}
return event;
});
}
}
引入:
import {HttpClientModule, HTTP_INTERCEPTORS} from '@angular/common/http';
providers: [
{provide: HTTP_INTERCEPTORS, useClass: JWTInterceptor, multi: true},
{provide: HTTP_INTERCEPTORS, useClass: ResponseInterceptor, multi: true}],
@angular/cli项目构建--interceptor的更多相关文章
- @angular/cli项目构建--组件
环境:nodeJS,git,angular/cli npm install -g cnpm --registry=https://registry.npm.taobao.org cnpm instal ...
- @angular/cli项目构建--modal
环境准备: cnpm install ngx-bootstrap-modal --save-dev impoerts: [BootstrapModalModule.forRoot({container ...
- @angular/cli项目构建--Dynamic.Form
导入所需模块: ReactiveFormsModule DynamicFormComponent.html <div [formGroup]="form"> <l ...
- @angular/cli项目构建--animations
使用方法一(文件形式定义): animations.ts import { animate, AnimationEntryMetadata, state, style, transition, tri ...
- @angular/cli项目构建--路由3
路由定位: modifyUser(user) { this.router.navigate(['/auction/users', user.id]); } 路由定义: {path: 'users/:i ...
- @angular/cli项目构建--httpClient
app.module.ts update imports: [ HttpClientModule] product.component.ts import {Component, OnInit} fr ...
- @angular/cli项目构建--路由2
app.module.ts update const routes: Routes = [ {path: '', redirectTo: '/home', pathMatch: 'full'}, {p ...
- @angular/cli项目构建--路由1
app.module.ts import {BrowserModule} from '@angular/platform-browser'; import {NgModule} from '@angu ...
- @angular/cli项目构建--Dynamic.Form(2)
form-item-control.service.ts update @Injectable() export class FormItemControlService { constructor( ...
随机推荐
- redis性能测试以及影响性能的因素
redis-benchmark测试工具的命令使使用方法及参数如下:redis-benchmark [-h <host>] [-p <port>] [-c <clients ...
- Numpy用于数组的文件输入输出
这一章比较简单,内容也比较少.而且对于文件的读写,还是使用pandas比较好.numpy主要是读写文本数据和二进制数据的. 将数组以二进制的格式保存到硬盘上 主要的函数有numpy.save和nump ...
- Linux文件IO
参考<unix高级环境编程> 本章开始讨论U N I X系统,先说明可用的文件I / O函数——打开文件.读文件.写文件等等.大多数U N I X文件I / O只需用到5个函数:o p e ...
- requirejs源码分析: config中shim
shim处理的源码: //Merge shim if (cfg.shim) { eachProp(cfg.shim, funct ...
- XSS插入绕过一些方式总结
详见:http://blog.csdn.net/keepxp/article/details/52054388 1 常规插入及其绕过 1.1 Script 标签 绕过进行一次移除操作: <scr ...
- Java生成json
JSON(JavaScript Object Notation):一种轻量级的数据交换格式: Be JSON:在线JSON校验格式化工具 www.bejson.com 需求:编写代码生成如下的json ...
- Python学习进程(7)字符串
本节介绍字符串的创建与操作方法. (1)创建字符串: 创建字符串既可以用单引号也可以用双引号: root@SJM:/home/sunjimeng/桌面# cat text.py ...
- Kubernetes StatefulSets
StatefulSets对于需要以下一项或多项的应用程序非常有用. 稳定,唯一的网络标识符. 稳定,持久的存储. 有序,优雅的部署和缩放. 有序,优雅的删除和终止. 有序的自动滚动更新. POD Id ...
- JAVA 使用qq邮箱发送邮件
引入一个架包: gradle( "com.sun.mail:javax.mail:1.5.6", ) 代码如下: private static final String QQ_EM ...
- CCNA 课程 一
OSI 参考模型: 7应用层 6表示层 5会话层 4传输层 -- TCP / UDP (端口号) 3网络层 -- IP (原IP地址,目标IP地址) 2数据链路层 -- ARPA / e ...