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的更多相关文章

  1. @angular/cli项目构建--组件

    环境:nodeJS,git,angular/cli npm install -g cnpm --registry=https://registry.npm.taobao.org cnpm instal ...

  2. @angular/cli项目构建--modal

    环境准备: cnpm install ngx-bootstrap-modal --save-dev impoerts: [BootstrapModalModule.forRoot({container ...

  3. @angular/cli项目构建--Dynamic.Form

    导入所需模块: ReactiveFormsModule DynamicFormComponent.html <div [formGroup]="form"> <l ...

  4. @angular/cli项目构建--animations

    使用方法一(文件形式定义): animations.ts import { animate, AnimationEntryMetadata, state, style, transition, tri ...

  5. @angular/cli项目构建--路由3

    路由定位: modifyUser(user) { this.router.navigate(['/auction/users', user.id]); } 路由定义: {path: 'users/:i ...

  6. @angular/cli项目构建--httpClient

    app.module.ts update imports: [ HttpClientModule] product.component.ts import {Component, OnInit} fr ...

  7. @angular/cli项目构建--路由2

    app.module.ts update const routes: Routes = [ {path: '', redirectTo: '/home', pathMatch: 'full'}, {p ...

  8. @angular/cli项目构建--路由1

    app.module.ts import {BrowserModule} from '@angular/platform-browser'; import {NgModule} from '@angu ...

  9. @angular/cli项目构建--Dynamic.Form(2)

    form-item-control.service.ts update @Injectable() export class FormItemControlService { constructor( ...

随机推荐

  1. mysql数据库中表的外键约束

    一.外键约束 1.什么是外键? 外键指的是其他表中的主键,当做该表的外键. 2.创建外键. 2.1 可在创建表的时候直接创建外键,如图所示: create table table_name  (字段名 ...

  2. 剑指offer 面试24题

    面试24题: 题目:反转链表 题:输入一个链表,反转链表并输出反转后链表的头节点. 解题思路:注意反转时出现断裂现象,定义3个指针,分别指向当前遍历到的节点pNode.它的前一个节点pPrev及后一个 ...

  3. sed Demo

    @1:sed basic usage: 和AWK一样, sed也是逐行对文本进行处理. sed的主要功能如下: @1:对每行中的匹配项进行处理(修改/删除) @2:格式化文本的处理 @3:(行的增删改 ...

  4. chrome浏览器插件让你浏览github的时候像IDE一样提供项目目录

    GitHub 作为代码托管平台,竟然没有提供项目目录,方便用户在线快速浏览项目结构.所以,在线分析项目源码就会变得很繁琐,必须一层一层点击,然后再一次一次地向上返回.要知道,本来 GitHub 网站在 ...

  5. 【LeetCode】【定制版排序】Sort Colors

    之前转载过一篇STL的sort方法底层详解的博客:https://www.cnblogs.com/ygh1229/articles/9806398.html 但是我们在开发中会根据自己特定的应用,有新 ...

  6. bootstrap table 复选框使用

    var columns = [ { field : 'checked', checkbox: true, align: 'center', valign: 'middle', formatter:fu ...

  7. 通过调节坐标进行jfree图的放大缩小

    http://blog.csdn.net/lt1983lt/article/details/5665085 import Java.awt.BorderLayout;import java.awt.C ...

  8. 使用easyui的form提交表单,在IE下出现类似附件下载时提示是否保存的现象

    之前开发时遇到的一个问题,使用easyui的form提交表单,在Chrome下时没问题的,但是在IE下出现类似附件下载时提示是否保存的现象. 这里记录一下如何解决的.其实这个现象不光是easyui的f ...

  9. CocoaPods安装使用

    $ gem sources --remove https://rubygems.org/ //等有反应之后再敲入以下命令 $ gem sources -a http://ruby.taobao.org ...

  10. webpack 从0 手动配置

    1. npm init 2. npm install -D webpack webpack-cli 3. 创建webpack入口文件( 默认 webpack.config.js 可以通过 webpac ...