Angular 2 http inteceptor doesn't work when using forRoot
up vote
-1
down vote
favorite
I have an http interceptor in my shared module. I need to share a service defined in the shared module as a singleton between multiple module, to do this I need to use forRoot in the app.module. However, when I do this my http interceptor no longer works.
Shared Module:
@NgModule({
imports: [
FormsModule,
ReactiveFormsModule,
CommonModule,
HttpModule
],
declarations: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives],
providers: [
{
provide: Http,
useFactory: fromFactories.httpFactory,
deps: [XHRBackend, RequestOptions, BaseDataService]
},
...fromServices.services,
...fromGuards.guards],
exports: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives]
})
App.module:
imports: [
SharedCoreModule.forRoot()
]
httpFactory:
export function httpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions, baseDataService: BaseDataService): Http {
return new InterceptedHttp(xhrBackend, requestOptions, baseDataService);
}
HttpInterceptor:
export class InterceptedHttp extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, public baseDataService: BaseDataService) {
super(backend, defaultOptions);
}
request(url: string | Request): Observable<Response> {
return super.request(url);
}
get(url: string, options): Observable<Response> {
return super.get(url, this.getRequestOptionArgs(options));
}
post(url: string, body: string, options): Observable<Response> {
return super.post(url, body, this.getRequestOptionArgs(options));
}
put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return super.put(url, body, this.getRequestOptionArgs(options));
}
delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
return super.delete(url, this.getRequestOptionArgs(options));
}
private getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
return this.baseDataService.getRequestOptionsNew(options);
}
}
Does anyone know why my interceptor no longer works?
angular typescript
|
show 1 more comment
up vote
-1
down vote
favorite
I have an http interceptor in my shared module. I need to share a service defined in the shared module as a singleton between multiple module, to do this I need to use forRoot in the app.module. However, when I do this my http interceptor no longer works.
Shared Module:
@NgModule({
imports: [
FormsModule,
ReactiveFormsModule,
CommonModule,
HttpModule
],
declarations: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives],
providers: [
{
provide: Http,
useFactory: fromFactories.httpFactory,
deps: [XHRBackend, RequestOptions, BaseDataService]
},
...fromServices.services,
...fromGuards.guards],
exports: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives]
})
App.module:
imports: [
SharedCoreModule.forRoot()
]
httpFactory:
export function httpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions, baseDataService: BaseDataService): Http {
return new InterceptedHttp(xhrBackend, requestOptions, baseDataService);
}
HttpInterceptor:
export class InterceptedHttp extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, public baseDataService: BaseDataService) {
super(backend, defaultOptions);
}
request(url: string | Request): Observable<Response> {
return super.request(url);
}
get(url: string, options): Observable<Response> {
return super.get(url, this.getRequestOptionArgs(options));
}
post(url: string, body: string, options): Observable<Response> {
return super.post(url, body, this.getRequestOptionArgs(options));
}
put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return super.put(url, body, this.getRequestOptionArgs(options));
}
delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
return super.delete(url, this.getRequestOptionArgs(options));
}
private getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
return this.baseDataService.getRequestOptionsNew(options);
}
}
Does anyone know why my interceptor no longer works?
angular typescript
Interceptors don't work with the deprecatedHttpModule
, you need to useHttpClientModule
. Also you don't actually seem to be including the interceptor.
– jonrsharpe
Nov 21 at 9:48
@jonrsharpe are you referring to Angular 2? As built in interceptors come in from Angular 4+. The above works when I don't import SharedModule with forRoot. The factory returns the interceptor.
– dotnethaggis
Nov 21 at 10:43
Please give a Minimal, Complete, and Verifiable example, then. Also I'd recommend not still using 2.x if that's actually what you're on.
– jonrsharpe
Nov 21 at 10:45
@jonrsharpe I have no choice about Angular 2... it's a working solution in a live production. Can't just upgrade from 2 - 7 :)
– dotnethaggis
Nov 21 at 10:50
1
You do, and you can. It would have been easier if you gradually upgraded instead of continuing to use a version that hasn't been updated in 18 months, but you can stop making that bad choice. See e.g. angular.io/guide/releases#support-policy-and-schedule - even 4 is out of LTS by now.
– jonrsharpe
Nov 21 at 10:53
|
show 1 more comment
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have an http interceptor in my shared module. I need to share a service defined in the shared module as a singleton between multiple module, to do this I need to use forRoot in the app.module. However, when I do this my http interceptor no longer works.
Shared Module:
@NgModule({
imports: [
FormsModule,
ReactiveFormsModule,
CommonModule,
HttpModule
],
declarations: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives],
providers: [
{
provide: Http,
useFactory: fromFactories.httpFactory,
deps: [XHRBackend, RequestOptions, BaseDataService]
},
...fromServices.services,
...fromGuards.guards],
exports: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives]
})
App.module:
imports: [
SharedCoreModule.forRoot()
]
httpFactory:
export function httpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions, baseDataService: BaseDataService): Http {
return new InterceptedHttp(xhrBackend, requestOptions, baseDataService);
}
HttpInterceptor:
export class InterceptedHttp extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, public baseDataService: BaseDataService) {
super(backend, defaultOptions);
}
request(url: string | Request): Observable<Response> {
return super.request(url);
}
get(url: string, options): Observable<Response> {
return super.get(url, this.getRequestOptionArgs(options));
}
post(url: string, body: string, options): Observable<Response> {
return super.post(url, body, this.getRequestOptionArgs(options));
}
put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return super.put(url, body, this.getRequestOptionArgs(options));
}
delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
return super.delete(url, this.getRequestOptionArgs(options));
}
private getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
return this.baseDataService.getRequestOptionsNew(options);
}
}
Does anyone know why my interceptor no longer works?
angular typescript
I have an http interceptor in my shared module. I need to share a service defined in the shared module as a singleton between multiple module, to do this I need to use forRoot in the app.module. However, when I do this my http interceptor no longer works.
Shared Module:
@NgModule({
imports: [
FormsModule,
ReactiveFormsModule,
CommonModule,
HttpModule
],
declarations: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives],
providers: [
{
provide: Http,
useFactory: fromFactories.httpFactory,
deps: [XHRBackend, RequestOptions, BaseDataService]
},
...fromServices.services,
...fromGuards.guards],
exports: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives]
})
App.module:
imports: [
SharedCoreModule.forRoot()
]
httpFactory:
export function httpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions, baseDataService: BaseDataService): Http {
return new InterceptedHttp(xhrBackend, requestOptions, baseDataService);
}
HttpInterceptor:
export class InterceptedHttp extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, public baseDataService: BaseDataService) {
super(backend, defaultOptions);
}
request(url: string | Request): Observable<Response> {
return super.request(url);
}
get(url: string, options): Observable<Response> {
return super.get(url, this.getRequestOptionArgs(options));
}
post(url: string, body: string, options): Observable<Response> {
return super.post(url, body, this.getRequestOptionArgs(options));
}
put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return super.put(url, body, this.getRequestOptionArgs(options));
}
delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
return super.delete(url, this.getRequestOptionArgs(options));
}
private getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
return this.baseDataService.getRequestOptionsNew(options);
}
}
Does anyone know why my interceptor no longer works?
angular typescript
angular typescript
edited Nov 21 at 10:52
asked Nov 21 at 9:46
dotnethaggis
796922
796922
Interceptors don't work with the deprecatedHttpModule
, you need to useHttpClientModule
. Also you don't actually seem to be including the interceptor.
– jonrsharpe
Nov 21 at 9:48
@jonrsharpe are you referring to Angular 2? As built in interceptors come in from Angular 4+. The above works when I don't import SharedModule with forRoot. The factory returns the interceptor.
– dotnethaggis
Nov 21 at 10:43
Please give a Minimal, Complete, and Verifiable example, then. Also I'd recommend not still using 2.x if that's actually what you're on.
– jonrsharpe
Nov 21 at 10:45
@jonrsharpe I have no choice about Angular 2... it's a working solution in a live production. Can't just upgrade from 2 - 7 :)
– dotnethaggis
Nov 21 at 10:50
1
You do, and you can. It would have been easier if you gradually upgraded instead of continuing to use a version that hasn't been updated in 18 months, but you can stop making that bad choice. See e.g. angular.io/guide/releases#support-policy-and-schedule - even 4 is out of LTS by now.
– jonrsharpe
Nov 21 at 10:53
|
show 1 more comment
Interceptors don't work with the deprecatedHttpModule
, you need to useHttpClientModule
. Also you don't actually seem to be including the interceptor.
– jonrsharpe
Nov 21 at 9:48
@jonrsharpe are you referring to Angular 2? As built in interceptors come in from Angular 4+. The above works when I don't import SharedModule with forRoot. The factory returns the interceptor.
– dotnethaggis
Nov 21 at 10:43
Please give a Minimal, Complete, and Verifiable example, then. Also I'd recommend not still using 2.x if that's actually what you're on.
– jonrsharpe
Nov 21 at 10:45
@jonrsharpe I have no choice about Angular 2... it's a working solution in a live production. Can't just upgrade from 2 - 7 :)
– dotnethaggis
Nov 21 at 10:50
1
You do, and you can. It would have been easier if you gradually upgraded instead of continuing to use a version that hasn't been updated in 18 months, but you can stop making that bad choice. See e.g. angular.io/guide/releases#support-policy-and-schedule - even 4 is out of LTS by now.
– jonrsharpe
Nov 21 at 10:53
Interceptors don't work with the deprecated
HttpModule
, you need to use HttpClientModule
. Also you don't actually seem to be including the interceptor.– jonrsharpe
Nov 21 at 9:48
Interceptors don't work with the deprecated
HttpModule
, you need to use HttpClientModule
. Also you don't actually seem to be including the interceptor.– jonrsharpe
Nov 21 at 9:48
@jonrsharpe are you referring to Angular 2? As built in interceptors come in from Angular 4+. The above works when I don't import SharedModule with forRoot. The factory returns the interceptor.
– dotnethaggis
Nov 21 at 10:43
@jonrsharpe are you referring to Angular 2? As built in interceptors come in from Angular 4+. The above works when I don't import SharedModule with forRoot. The factory returns the interceptor.
– dotnethaggis
Nov 21 at 10:43
Please give a Minimal, Complete, and Verifiable example, then. Also I'd recommend not still using 2.x if that's actually what you're on.
– jonrsharpe
Nov 21 at 10:45
Please give a Minimal, Complete, and Verifiable example, then. Also I'd recommend not still using 2.x if that's actually what you're on.
– jonrsharpe
Nov 21 at 10:45
@jonrsharpe I have no choice about Angular 2... it's a working solution in a live production. Can't just upgrade from 2 - 7 :)
– dotnethaggis
Nov 21 at 10:50
@jonrsharpe I have no choice about Angular 2... it's a working solution in a live production. Can't just upgrade from 2 - 7 :)
– dotnethaggis
Nov 21 at 10:50
1
1
You do, and you can. It would have been easier if you gradually upgraded instead of continuing to use a version that hasn't been updated in 18 months, but you can stop making that bad choice. See e.g. angular.io/guide/releases#support-policy-and-schedule - even 4 is out of LTS by now.
– jonrsharpe
Nov 21 at 10:53
You do, and you can. It would have been easier if you gradually upgraded instead of continuing to use a version that hasn't been updated in 18 months, but you can stop making that bad choice. See e.g. angular.io/guide/releases#support-policy-and-schedule - even 4 is out of LTS by now.
– jonrsharpe
Nov 21 at 10:53
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409213%2fangular-2-http-inteceptor-doesnt-work-when-using-forroot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Interceptors don't work with the deprecated
HttpModule
, you need to useHttpClientModule
. Also you don't actually seem to be including the interceptor.– jonrsharpe
Nov 21 at 9:48
@jonrsharpe are you referring to Angular 2? As built in interceptors come in from Angular 4+. The above works when I don't import SharedModule with forRoot. The factory returns the interceptor.
– dotnethaggis
Nov 21 at 10:43
Please give a Minimal, Complete, and Verifiable example, then. Also I'd recommend not still using 2.x if that's actually what you're on.
– jonrsharpe
Nov 21 at 10:45
@jonrsharpe I have no choice about Angular 2... it's a working solution in a live production. Can't just upgrade from 2 - 7 :)
– dotnethaggis
Nov 21 at 10:50
1
You do, and you can. It would have been easier if you gradually upgraded instead of continuing to use a version that hasn't been updated in 18 months, but you can stop making that bad choice. See e.g. angular.io/guide/releases#support-policy-and-schedule - even 4 is out of LTS by now.
– jonrsharpe
Nov 21 at 10:53