what does the multi: true attribute of HTTP_INTERCEPTORS mean?

Multi tool use
what does the multi: true attribute of HTTP_INTERCEPTORS mean?
I'm new to Angular and I've just built an interceptor. According to multiple tutorials you have to include the HTTP_INTERCEPTORS
in the app.module
like so:
HTTP_INTERCEPTORS
app.module
providers: [{ provide: HTTP_INTERCEPTORS, useClass: MyInterceptor, multi: true }]
I was wondering what that multi: true
attribute means/does and whether it can be omitted or not.
multi: true
I've read the angular.io guide about this attribute. They explain it as following:
I don't understand this part:
Note the multi: true option. This required setting tells Angular that
HTTP_INTERCEPTORS is a token for a multiprovider that injects an array
of values, rather than a single value.
This sheds some light on the concept but I don't really understand yet when an interceptor is injecting multiple values and when it isn't. For example, my own interceptor is only changing the headers. Does this mean its injecting only a single value?
Thank you
EDIT:
This is my interceptor
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { LoginService } from '../Services/login.service';
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
constructor(private loginService:LoginService){}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// add authorization header with jwt token if available
console.log("ik zit nu in de interceptor");
let currentUser = this.loginService.getToken();
if (currentUser !=="") {
request = request.clone({
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': `Bearer ${currentUser}`
})
});
}
return next.handle(request);
}
}
This is the provide list of app.module
providers: [
{ provide: APP_BASE_HREF, useValue: '/' },
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
{ provide: 'AUTH_URL', useValue: 'http://localhost:8080/auth' },
{ provide: 'API_URL', useValue: 'http://localhost:8080/api' },
{ provide: 'HEADERS', useValue: new HttpHeaders({'Content-Type': 'application/json'}) },
LoginGuard,
LoginService,
UserService,
MessageService
],
I'm aware of the angular.io guide but some parts i don't understand, hence i asked on stackoverflow. There is no need to punish me with downvotes just for asking a small question. I don't understand this part:
Note the multi: true option. This required setting tells Angular that HTTP_INTERCEPTORS is a token for a multiprovider that injects an array of values, rather than a single value.
. When i use the request clone method in my interceptor i am altering just the headers. Does this mean i am injecting only a single value and therefore can put Multi to false? Thank you– Maurice
Jul 2 at 17:41
Note the multi: true option. This required setting tells Angular that HTTP_INTERCEPTORS is a token for a multiprovider that injects an array of values, rather than a single value.
Why didn't explain that in the post to being with? What you posted makes it look like you hadn't done any research at all.
– R. Richards
Jul 2 at 17:46
If you read the docs and just didn't understand a specific part, why didn't you ask about the specific part?
– Ingo Bürk
Jul 2 at 17:46
i've changed my post. Hope its more satisfactory now.
– Maurice
Jul 2 at 17:50
1 Answer
1
From the description of ValueProvider interface we can read for the multi
property:
multi
If true, then injector returns an array of instances. This is useful
to allow multiple providers spread across many files to provide
configuration information to a common token.
This mean that for the token we are providing a value, more than one value (or class) is going to be used.
For instance see this example (this is a sample project) where it is specified for the token HTTP_INTERCEPTORS
to use the class (useClass
) ErrorInterceptor
and SecurityInterceptor
. In order to get this working we need to specify multi: true
in order to let Angular know that multiple values (or class) are going to be used.
HTTP_INTERCEPTORS
useClass
ErrorInterceptor
SecurityInterceptor
multi: true
{
provide: HTTP_INTERCEPTORS,
useClass: ErrorInterceptor,
multi: true
},
{
provide: HTTP_INTERCEPTORS,
useClass: SecurityInterceptor,
multi: true
},
A key point here is that HTTP_INTERCEPTORS
is a multi-provider token. This means that when providing a new value or class for this token the property multi
is required and it must be set to true
.
HTTP_INTERCEPTORS
multi
true
See in the HttpClient documentation when it is described how to provide an interceptor the part where it is stated:
Note the multi: true
option. This required setting tells Angular that
HTTP_INTERCEPTORS is a token for a multiprovider that injects an array
of values, rather than a single value.
multi: true
ok so when i have only one Interceptor i can put Multi to false right?
– Maurice
Jul 2 at 17:53
when i put Multi to false i get this error:
Mixing multi and non multi provider is not possible for token InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1
. I don't understand because there are no other interceptors present in the providers list. I do provide APP_BASE_REF but i'm not sure if that has anything to do with the error. Its really strange. Guess i better put it back to true then.– Maurice
Jul 2 at 18:00
Mixing multi and non multi provider is not possible for token InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1
@Maurice, Yes, so far I've never seen providing an interceptor with this property to
false
or omitted. Not being so with other providers, where you can even omit this property. It would be interesting to see an scenario as the one you describe (if possible at all), where a value or class is used to provide an HTTP_INTERCEPTORS
with multi
to false, though. Maybe if you have a github repo, drop a link and I'll take a look at it.– lealceldeiro
Jul 2 at 18:05
false
HTTP_INTERCEPTORS
multi
please see my opening post, i've placed the interceptor and provide list in it.
– Maurice
Jul 2 at 18:15
@Maurice after digging a bit more in the angular documentation I found that in the case for the token
HTTP_INTERCEPTORS
this property is required and must be set to true
because it is a multiprovider token. I updated my answer, see the last part.– lealceldeiro
Jul 2 at 18:28
HTTP_INTERCEPTORS
true
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
angular.io/guide/http#provide-the-interceptor
– R. Richards
Jul 2 at 17:35