How to import Angular HTTP interceptor only for Child module










0















I have imported HttpClientModule in AppModule.



import HttpClientModule from '@angular/common/http';

export const AppRoutes: Routes = [
path: '', redirectTo: 'dashboard', pathMatch: 'full' ,

path: 'account',
loadChildren: './auth/auth.module#AuthModule'
,

path: 'dashboard',
loadChildren: './content/content.module#ContentModule'

];


Once I launch site, it will redirect to dashboard module(Lazy loaded module) where i added http interceptor.



import HTTP_INTERCEPTORS from '@angular/common/http';
import HttpInterceptorService from '../services/http-interceptor.service';
@NgModule(
providers : [
provide: HTTP_INTERCEPTORS, useClass: HttpInterceptorService, multi: true
]
)


But it doesn't working. Can anyone help me?










share|improve this question






















  • post appmodule ts

    – Sajeetharan
    Nov 14 '18 at 17:27















0















I have imported HttpClientModule in AppModule.



import HttpClientModule from '@angular/common/http';

export const AppRoutes: Routes = [
path: '', redirectTo: 'dashboard', pathMatch: 'full' ,

path: 'account',
loadChildren: './auth/auth.module#AuthModule'
,

path: 'dashboard',
loadChildren: './content/content.module#ContentModule'

];


Once I launch site, it will redirect to dashboard module(Lazy loaded module) where i added http interceptor.



import HTTP_INTERCEPTORS from '@angular/common/http';
import HttpInterceptorService from '../services/http-interceptor.service';
@NgModule(
providers : [
provide: HTTP_INTERCEPTORS, useClass: HttpInterceptorService, multi: true
]
)


But it doesn't working. Can anyone help me?










share|improve this question






















  • post appmodule ts

    – Sajeetharan
    Nov 14 '18 at 17:27













0












0








0








I have imported HttpClientModule in AppModule.



import HttpClientModule from '@angular/common/http';

export const AppRoutes: Routes = [
path: '', redirectTo: 'dashboard', pathMatch: 'full' ,

path: 'account',
loadChildren: './auth/auth.module#AuthModule'
,

path: 'dashboard',
loadChildren: './content/content.module#ContentModule'

];


Once I launch site, it will redirect to dashboard module(Lazy loaded module) where i added http interceptor.



import HTTP_INTERCEPTORS from '@angular/common/http';
import HttpInterceptorService from '../services/http-interceptor.service';
@NgModule(
providers : [
provide: HTTP_INTERCEPTORS, useClass: HttpInterceptorService, multi: true
]
)


But it doesn't working. Can anyone help me?










share|improve this question














I have imported HttpClientModule in AppModule.



import HttpClientModule from '@angular/common/http';

export const AppRoutes: Routes = [
path: '', redirectTo: 'dashboard', pathMatch: 'full' ,

path: 'account',
loadChildren: './auth/auth.module#AuthModule'
,

path: 'dashboard',
loadChildren: './content/content.module#ContentModule'

];


Once I launch site, it will redirect to dashboard module(Lazy loaded module) where i added http interceptor.



import HTTP_INTERCEPTORS from '@angular/common/http';
import HttpInterceptorService from '../services/http-interceptor.service';
@NgModule(
providers : [
provide: HTTP_INTERCEPTORS, useClass: HttpInterceptorService, multi: true
]
)


But it doesn't working. Can anyone help me?







angular http httpclient interceptor angular-http-interceptors






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 17:24









IndhuIndhu

2671315




2671315












  • post appmodule ts

    – Sajeetharan
    Nov 14 '18 at 17:27

















  • post appmodule ts

    – Sajeetharan
    Nov 14 '18 at 17:27
















post appmodule ts

– Sajeetharan
Nov 14 '18 at 17:27





post appmodule ts

– Sajeetharan
Nov 14 '18 at 17:27












1 Answer
1






active

oldest

votes


















0














Http interceptor is just a service there won't be any difference in injecting it on any modules - just do the same in AppModule that will help you to work - if in case you have the same problem you can try injecting like below on your shared module



The common pattern to achieve that is not to expose providers directly on the @NgModule declaration but in a static forRoot function like that:



export class SharedModule 
static forRoot(): ModuleWithProviders
return
ngModule: SharedModule,
providers: [
provide: HTTP_INTERCEPTORS, useClass: HttpInterceptorService, multi: true
]
;




You can then import your SharedModule on AppModule like SharedModule.forRoot()



Hope this works - Happy coding






share|improve this answer























  • Thanks for your effort. But actually it works after sometime.

    – Indhu
    Nov 15 '18 at 9:46











  • No problem :) Happy coding

    – Rahul
    Nov 15 '18 at 9:52










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305685%2fhow-to-import-angular-http-interceptor-only-for-child-module%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Http interceptor is just a service there won't be any difference in injecting it on any modules - just do the same in AppModule that will help you to work - if in case you have the same problem you can try injecting like below on your shared module



The common pattern to achieve that is not to expose providers directly on the @NgModule declaration but in a static forRoot function like that:



export class SharedModule 
static forRoot(): ModuleWithProviders
return
ngModule: SharedModule,
providers: [
provide: HTTP_INTERCEPTORS, useClass: HttpInterceptorService, multi: true
]
;




You can then import your SharedModule on AppModule like SharedModule.forRoot()



Hope this works - Happy coding






share|improve this answer























  • Thanks for your effort. But actually it works after sometime.

    – Indhu
    Nov 15 '18 at 9:46











  • No problem :) Happy coding

    – Rahul
    Nov 15 '18 at 9:52















0














Http interceptor is just a service there won't be any difference in injecting it on any modules - just do the same in AppModule that will help you to work - if in case you have the same problem you can try injecting like below on your shared module



The common pattern to achieve that is not to expose providers directly on the @NgModule declaration but in a static forRoot function like that:



export class SharedModule 
static forRoot(): ModuleWithProviders
return
ngModule: SharedModule,
providers: [
provide: HTTP_INTERCEPTORS, useClass: HttpInterceptorService, multi: true
]
;




You can then import your SharedModule on AppModule like SharedModule.forRoot()



Hope this works - Happy coding






share|improve this answer























  • Thanks for your effort. But actually it works after sometime.

    – Indhu
    Nov 15 '18 at 9:46











  • No problem :) Happy coding

    – Rahul
    Nov 15 '18 at 9:52













0












0








0







Http interceptor is just a service there won't be any difference in injecting it on any modules - just do the same in AppModule that will help you to work - if in case you have the same problem you can try injecting like below on your shared module



The common pattern to achieve that is not to expose providers directly on the @NgModule declaration but in a static forRoot function like that:



export class SharedModule 
static forRoot(): ModuleWithProviders
return
ngModule: SharedModule,
providers: [
provide: HTTP_INTERCEPTORS, useClass: HttpInterceptorService, multi: true
]
;




You can then import your SharedModule on AppModule like SharedModule.forRoot()



Hope this works - Happy coding






share|improve this answer













Http interceptor is just a service there won't be any difference in injecting it on any modules - just do the same in AppModule that will help you to work - if in case you have the same problem you can try injecting like below on your shared module



The common pattern to achieve that is not to expose providers directly on the @NgModule declaration but in a static forRoot function like that:



export class SharedModule 
static forRoot(): ModuleWithProviders
return
ngModule: SharedModule,
providers: [
provide: HTTP_INTERCEPTORS, useClass: HttpInterceptorService, multi: true
]
;




You can then import your SharedModule on AppModule like SharedModule.forRoot()



Hope this works - Happy coding







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 17:42









RahulRahul

1,0972318




1,0972318












  • Thanks for your effort. But actually it works after sometime.

    – Indhu
    Nov 15 '18 at 9:46











  • No problem :) Happy coding

    – Rahul
    Nov 15 '18 at 9:52

















  • Thanks for your effort. But actually it works after sometime.

    – Indhu
    Nov 15 '18 at 9:46











  • No problem :) Happy coding

    – Rahul
    Nov 15 '18 at 9:52
















Thanks for your effort. But actually it works after sometime.

– Indhu
Nov 15 '18 at 9:46





Thanks for your effort. But actually it works after sometime.

– Indhu
Nov 15 '18 at 9:46













No problem :) Happy coding

– Rahul
Nov 15 '18 at 9:52





No problem :) Happy coding

– Rahul
Nov 15 '18 at 9:52



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305685%2fhow-to-import-angular-http-interceptor-only-for-child-module%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto