gpt4 book ai didi

Angular 通用 HttpInterceptor 运行两次

转载 作者:行者123 更新时间:2023-12-03 00:33:08 25 4
gpt4 key购买 nike

我目前正在将我的 Angular 应用程序切换到通用 ssr 应用程序。我的最后一个问题是我的自定义 HttpInterceptor 触发了两次。我正在检查 api 调用的 http 响应代码,如果有错误,则会显示弹出窗口。自从我切换到通用后,我两次弹出相同的错误。

@Injectable()
export class HttpSetHeaders implements HttpInterceptor {
constructor(
@Inject(PLATFORM_ID) private platformId,
@Inject(LOCAL_STORAGE) private localStorage: any,
private authService: AuthService,
private router: Router,
private notifierService: NotifierService
) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log(isPlatformServer(this.platformId))
......
}

每次 HTTPClient 调用后,我都会看到控制台日志输出两次

我已经在我的 app.module 中导入了 TransferHttpCacheModule和我的 app.server.module 中的 TransferHttpCacheModule

谢谢马库斯

最佳答案

根据https://angular.io/api/common/http/HttpInterceptor

To use the same instance of HttpInterceptors for the entire app, import the HttpClientModule only in your AppModule, and add the interceptors to the root application injector . If you import HttpClientModule multiple times across different modules (for example, in lazy loading modules), each import creates a new copy of the HttpClientModule, which overwrites the interceptors provided in the root module.

请告诉我这是否可以解决您的问题。可能您只是多次实例化 HttpInterceptor。

关于Angular 通用 HttpInterceptor 运行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58818604/

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