Reputation: 2064
How do I inject dependencies when exporting a function instead a class?
Upvotes: 3
Views: 677
Reputation: 26406
Add an inject
property listing your dependencies to your constructor function:
import {EventAggregator} from 'aurelia-event-aggregator';
export function Example(eventAggregator) {
console.log(eventAggregator);
return {
message: 'hello world'
};
}
Example.inject = [EventAggregator];
Running example: https://gist.run/?id=d60c5c7dfbf53e507aae47d6c05b7d36
If you'd like to use the inject
decorator manually instead of adding the static inject
property you can write:
import {EventAggregator} from 'aurelia-event-aggregator';
import {inject} from 'aurelia-dependency-injection';
export function Example(eventAggregator) {
console.log(eventAggregator);
return {
message: 'hello world'
};
}
inject(EventAggregator)(Example);
note: standard decorator "@" syntax requires using ES6 classes so you may want to modernize.
Upvotes: 5