angular - HttpInterceptor 在 Angular 6 中不起作用
问题描述
我正在使用HttpInterceptor
添加身份验证令牌,问题是拦截器正在为第一个请求添加令牌,然后它停止工作。这是我的拦截器服务
import { Injectable } from '@angular/core';
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
import {Observable} from "rxjs/internal/Observable";
import {CommonService} from "./common.service";
@Injectable({
providedIn: 'root'
})
export class AuthInterceptorService implements HttpInterceptor {
constructor(private auth: CommonService) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authToken = this.auth.getAuthToken();
const authReq = req.clone({ setHeaders: { Authorization: 'Bearer '+authToken.access_token} });
return next.handle(authReq);
}
}
这是应用程序模块提供程序部分
providers: [{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptorService,
multi: true
}],
让我知道代码有什么问题。
解决方案
推荐阅读
- javascript - 在节点请求中访问函数内部变量
- visual-studio-2015 - 使用 ORCA 工具将 MSI 设置为中文
- android - iOS 上的 CSS 与 Android 上的不同?
- java - Spring initializr 项目不适用于多个错误
- c# - 在哪里存储可以作为控制台应用程序或服务运行的应用程序的配置文件
- java - 如何在不更改代码且仅更改数据库的情况下在应用程序中进行功能更改?
- android - 报警管理器和位置管理器
- qt - 将拖动选择限制为 QTableWidget 中的单行
- php - 将 http 流量重定向到 https,避免混合内容警告
- rust - 字符串和格式的借用值不够长