【发布时间】:2025-12-13 17:45:02
【问题描述】:
在我的拦截器中,如果响应 statusCode 为 401,我想在响应上放置一个管道并注销,但问题是当我放置一个 catchError 时,observable 不是 HttpEvent 类型并且拦截函数期望返回该类型?
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authToken = `Agency ${this.auth.token}`;
const requestWithHeaders = req.clone({ setHeaders: {'Content-Type': 'application/json'}});
if (!!this.auth.token) {
const authReq = requestWithHeaders.clone({ setHeaders: { Authorization: authToken} });
return next.handle(authReq).pipe(
catchError(er => {
if (er.status === 401) {
this.auth.logout();
}
return er;
})
);
} else {
return next.handle(requestWithHeaders);
}
}
}。
【问题讨论】:
-
创建注销路径并导航。
标签: angular typescript angular-http-interceptors