Angular 5 Interceptor不会拦截来自库的请求 [英] Angular 5 Interceptor doesn't intercept requests made from a library

查看:112
本文介绍了Angular 5 Interceptor不会拦截来自库的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular 5中有一个有效的拦截器.它已在App.module的提供程序中注册,并正确拦截了从应用程序发出的所有请求.

I have a working interceptor in Angular 5. It's registered in App.module's providers and correctly intercepts all requests made from the application.

问题在于它不会拦截应用程序使用的库发出的请求.

The problem is that it doesn't intercept the requests made from the libraries used by the app.

我正在使用开放源代码库(NGX-Jsonapi),并且需要拦截器在该库向后端发出的每个请求中提供令牌.

I'm using an open source library (NGX-Jsonapi), and need the interceptor to provide a token in every request that the library makes to the back-end.

有人遇到同样的问题吗?

Someone faced the same issue?

该库使用HttpClient.

the library uses HttpClient.

推荐答案

最后,我在 Angular HttpInterceptor文档使用说明:

要在整个应用中使用相同的HttpInterceptors实例, 仅在您的AppModule中导入HttpClientModule,然后添加 根应用程序注入器的拦截器.如果导入 跨不同模块的HttpClientModule多次(例如, 在延迟加载模块中),每次导入都会创建新的 HttpClientModule,它将覆盖 根模块.

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.

我正在向htte请求进行延迟加载的模块中导入HttpClientModule.解决此问题后,所有内容都可以发挥作用.

I was importing HttpClientModule in a lazy loaded module that was making hte requests. After resolving this issue everything works like a charm.

这篇关于Angular 5 Interceptor不会拦截来自库的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆