What does a multi-provider do?
Multi-provider lets you register several values under the same token. Angular does not replace the old values, it collects them into an array.
Example:
ts
providers: [
{ provide: 'LOGGERS', useValue: 'console', multi: true },
{ provide: 'LOGGERS', useValue: 'file', multi: true },
{ provide: 'LOGGERS', useValue: 'cloud', multi: true }
]Now, injecting 'LOGGERS':
ts
constructor(@Inject('LOGGERS') loggers: string[]) {
console.log(loggers); // ['console', 'file', 'cloud']
}When it is used:
- for settings and plugins, where a list of handlers needs to be collected;
- when several modules add their own data to the same token;
- to extend the system easily without rewriting code.
Summary:
multi: true tells Angular:
"Do not replace the old value, add the new one to this token, and return everything as an array."
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.