HTTP_INTERCEPTORS only in AppModule

泄露秘密 提交于 2019-12-25 01:34:06

问题


I am trying to implement lazy loading in my Angular 6 app, all of my http calls are made in the FeatureModule (lazy loaded), but still I have to add HttpClientModule in my AppModule and not in FeatureModule. Didn't really understand why. Also, when I added interceptors in my FeatureModule, they didn't intercept any request. I have to add it in the AppModule only (I guess, it is because HttpClientModule is in AppModule).

I want to understand why this is the case?? Why can't we have HttpClientModule and HTTP_INTERCEPTORS only in the FeatureModule and not in AppModule where I am not making any http calls?


回答1:


To answer this question.

Only add HttpClientModule at once place at the root level. No other modules regardless of how they are imported lazy or eager even if it is from a library. Ensure that it is not importing HttpClientModule

You can however use HTTP_INTERCEPTORS providers across modules without an issue.

When you import HttpClientModule across modules and then re import that into another module it effectively resets the providers for HTTP_INTERCEPTORS for that module. This is an intended behaviour. If you need more information about the discussion you can check: https://github.com/angular/angular/issues/20575



来源:https://stackoverflow.com/questions/53344221/http-interceptors-only-in-appmodule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!