In my Angular (4+) application, I want to create a basic plugin mechanism:
Example: interface SearchStrategy
with implementations PersonSearchStrategy
and DocumentSearchStrategy
(both services, registered as providers).
Is there a way to dynamically get instances of these services by querying for implementations of the SearchStrategy
interface? How?
(might be some Injector related functionality?)
It's kinda possible, provided if you register the services with InjectionToken
and use provide multi
.
You can create an injection token with interface.
export const SearchStrategyToken = new InjectionToken<SearchStrategy[]>('SearchStrategy');
In your module register:
providers: [
{provide: SearchStrategyToken, useClass: PersonSearchStrategy, multi: true},
{provide: SearchStrategyToken, useClass: DocumentSearchStrategy, multi: true},
]
In your component or service:
constructor(private injector: Injector) {
const services = this.injector
.get(SearchStrategyToken); // return 2 items [ PersonSearchStrategy, DocumentSearchStrategy ]
const personSearch = services.find(x => x.constructor.name === 'PersonSearchStrategy');
const docSearch = services.find(x => x.constructor.name === 'DocumentSearchStrategy');
}
Code example provided here: https://stackblitz.com/edit/angular-clxr6k.
However, it would be good if you provide more details on your use case. Probably there are better solution than going for the above route.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With