gpt4 book ai didi

javascript - 如何在不使用 ES6 类功能的情况下在 Aurelia 中注入(inject)依赖项

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:48 25 4
gpt4 key购买 nike

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

最佳答案

添加一个 inject 属性,列出您对构造函数的依赖关系:

import {EventAggregator} from 'aurelia-event-aggregator';

export function Example(eventAggregator) {
console.log(eventAggregator);
return {
message: 'hello world'
};
}

Example.inject = [EventAggregator];

运行示例:https://gist.run/?id=d60c5c7dfbf53e507aae47d6c05b7d36

如果您想手动使用 inject 装饰器而不是添加静态 inject 属性,您可以这样写:

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.

关于javascript - 如何在不使用 ES6 类功能的情况下在 Aurelia 中注入(inject)依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39122600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com