如何将拦截器注入到 Angular 17 中作为模块化应用程序生成的应用程序中?我的意思是用 --standalone 参数生成的?
我尝试将其注入为 [provideHttpClient(withInterceptors([AuthInterceptor]))] 但没有成功。应用程序进入无限循环。
如果您可以提供一些有关您正在使用的拦截器类型的信息,将会很有帮助。
如果是 Angular 15 中引入的较新的 功能拦截器,您应该按照您在问题中建议的方式提供它。您可以确定是否使用功能拦截器,因为它将实现
HttpInterceptorFn
接口。
提供这样的功能拦截器:
providers: [
provideHttpClient(withInterceptors([authenticationInterceptor]))
]
如果您正在使用旧的基于类的拦截,那么您应该继续以传统方式提供它。
提供基于类的拦截器,如下所示::
providers: [
provideHttpClient(withInterceptorsFromDi())
]