Performance: Multi-provider pattern
This pattern affects the dependency injection system's initialization and memory usage, impacting initial load and runtime injection speed.
Jump into concepts and practice - no test required
providers: [
{ provide: 'API_URL', useValue: 'https://api1.example.com' },
{ provide: 'API_URL_MULTI', multi: true, useValue: 'https://api2.example.com' },
{ provide: 'API_URL_MULTI', multi: true, useValue: 'https://api3.example.com' }
]providers: [
{ provide: 'API_URL', useValue: 'https://api1.example.com' },
{ provide: 'API_URL', useValue: 'https://api2.example.com' },
{ provide: 'API_URL', useValue: 'https://api3.example.com' }
]| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple single providers for same token | N/A | N/A | N/A | [X] Bad |
| Multi-provider token aggregation | N/A | N/A | N/A | [OK] Good |
multi: true set.multi: true for both providers. providers: [{ provide: TOKEN, useClass: ServiceA }, { provide: TOKEN, useClass: ServiceB }] misses multi, options with multi: false or invalid strings like 'yes' do not work.providers: [
{ provide: 'TOKEN', useValue: 'A', multi: true },
{ provide: 'TOKEN', useValue: 'B', multi: true }
]inject('TOKEN')?multi: true, Angular injects an array of all values in registration order.providers: [
{ provide: TOKEN, useClass: ServiceA },
{ provide: TOKEN, useClass: ServiceB, multi: true }
]multi: true. Missing it on one disables multi-provider behavior.multi: true, so Angular treats it as a single provider, overriding others. Only ServiceB with multi: true is ignored.multi: true. Angular injects all as an array, allowing combined logging without modifying existing code.