简体   繁体   中英

How to inject dependencies in Aurelia without using ES6 class feature

在导出函数而不是类时,如何注入依赖项?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM