You need to register your service providers like this:
boostrap(AppComponent, [
provide(MyInterface, { useClass: Service1, multi:true });
provide(MyInterface, { useClass: Service2, multi:true });
]);
This will work only with classes not with interfaces since interfaces don't exist at runtime.
To make it work with interfaces, you need to adapt it:
bootstrap(AppComponent, [
provide('MyInterface', { useClass: Service1, multi:true }),
provide('MyInterface', { useClass: Service2, multi:true }),
CollectorService
]);
and inject this way:
@Injectable()
export class CollectorService {
constructor(@Inject('MyInterface') services:MyInterface[]) {
services.forEach(s => s.foo());
}
}
See this plunkr for more details: https://plnkr.co/edit/HSqOEN?p=preview.
See this link for more details:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…