gpt4 book ai didi

angular - 当我提供拦截器时获取 'Can' t 解析所有参数

转载 作者:太空狗 更新时间:2023-10-29 17:01:46 26 4
gpt4 key购买 nike

我正在尝试实现 HttpInterceptor。当我将它添加到@NgModule 时,我在 Chrome 中收到以下错误:

Uncaught Error: Can't resolve all parameters for JwtInterceptor: (?, ?).
at syntaxError (compiler.js:466)
at CompileMetadataResolver._getDependenciesMetadata (compiler.js:15547)
at CompileMetadataResolver._getTypeMetadata (compiler.js:15382)

花了很多时间在 google 上,不知道该做什么......

这是我在 AppModule 中提供拦截器的方式:

...
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: JwtInterceptor,
multi: true
}
],
...

这是拦截器本身,没什么特别的:

export class JwtInterceptor implements HttpInterceptor {
constructor(private inj: Injector, private logger: Logger) {
this.logger.l(true)('Interceptor >>');
}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.logger.l(true)('interceptor >>');

const auth = this.inj.get(AuthService);

// add token to the request:
const authReq = req.clone({
setHeaders: {
Authorization: `Bearer ${auth.getToken()}`
}
});

// return next.handle(authReq);
return next.handle(authReq).do((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// todo: get refreshed token if it exists:
}
}, (err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
auth.collectFailedRequest(authReq);
// todo: redirect to the login route or show a modal
}
}
});
}
}

最佳答案

糟糕,我忘了将 @Injectable() 添加到我的 interceptor 类中。

关于angular - 当我提供拦截器时获取 'Can' t 解析所有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47675757/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com