@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( ...
随机推荐
- Spring学习笔记4—流程(Spring Web Flow)
Spring Web Flow是Spring框架的子项目,作用是让程序按规定流程运行. 1 安装配置Spring Web Flow 虽然Spring Web Flow是Spring框架的子项目,但它并 ...
- Python爬虫:获取新浪网新闻
代码 #coding:utf-8 import requests from bs4 import BeautifulSoup res = requests.get("http://news. ...
- 爬虫五 Beautifulsoup模块
一 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你 ...
- jquery datepicker日历控件
地址:http://jqueryui.com/datepicker/ 使用:$( "#datepicker" ).datepicker(); $.datepicker.setDef ...
- Web Deploy 安装及问题解决
注意: 站点名称: 服务器上IIS的站点名称. . 我之前这里随便写一直不成功. 返回500..... 用户名, 密码: 这里最好用windows帐号. 问题比较少. 目标URL: 可不写. 可 ...
- LDA主题模型三连击-入门/理论/代码
目录 概况 为什么需要 LDA是什么 LDA的应用 gensim应用 数学原理 预备知识 抽取模型 样本生成 代码编写 本文将从三个方面介绍LDA主题模型--整体概况.数学推导.动手实现. 关于LDA ...
- lambda可调用对象
//find_if谓词使用 bool isShorter(const string &s1, const string &sz){ return s1.size() < sz.s ...
- Linux Shell编程 test命令
概述 test 命令是Shell 脚本中用来进行条件判断的. test命令示例 按照文件类型进行判断 测试选项 作 用 -b 文件 判断该文件是否存在,并且是否为块设备文件(是块设备文件为真) -c ...
- docker calico安装
第一步,安装etcd: 请参考以前的文章: http://www.cnblogs.com/vincenshen/articles/8637949.html 第二步,下载calico: sudo ...
- DB2日期及时间的使用
1.DB2中日期.时间寄存器的使用 --取日期 SELECT CURRENT DATE FROM sysibm.sysdummy1; --返回数据类型:DATE --结果集(YYYY-MM-DD): ...