zoukankan      html  css  js  c++  java
  • angular之intercepter

    import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
    import { Injectable } from '@angular/core';
    import { Observable } from 'rxjs';
    
    @Injectable()
    export class TokenInterceptor implements HttpInterceptor {
      intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        request = request.clone({
          setHeaders: {
            Authorization: `${localStorage.token}`
          }
        });
    
        return next.handle(request);
      }
    }
    
    // app.module.ts
    providers: [
        {
          provide: HTTP_INTERCEPTORS,
          useClass: TokenInterceptor,
          multi: true
        }
      ]
    

    然后尴尬的一幕出现了,intercepter只在登录前被成功调用了,要用到sessionToken的时候反而罢工了。
    google了半天,发现不能有多个HttpClientModule声明。

    --------------------------- 知道的更多,不知道的也更多 ---------------------------
  • 相关阅读:
    express框架总结
    http协议和file协议的区别
    苹果和安卓机的兼容问题
    nodejs搭建服务器
    VsCode编辑器
    编辑器统一 快捷键
    前后端分离 方案
    资源
    commonJs的运行时加载和es6的编译时加载
    mock 数据 解决方案
  • 原文地址:https://www.cnblogs.com/mryux/p/15477300.html
Copyright © 2011-2022 走看看