Lew*_*wis 4 aurelia aurelia-framework
在我的Aurelia SPA中,我有一些要在不同模块中使用的功能。它依赖于调用时给定的参数和单例的参数。有没有一种创建导出函数的方法,我可以在每次调用该函数时将我的Auth单例注入而不必将其作为参数传递?我想要它看起来的一个简单例子就是这个。
import Auth from './auth/storage';
import { inject } from 'aurelia-framework';
@inject(Auth)
export function A(foo: boolean): boolean {
let auth = Auth;
if (auth.authorized && foo) {
return true
}
else {
return false
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以将其包装在一个类中并使用它,但想知道是否有任何方法可以实现这一点
如果要在函数中使用依赖项注入,请使用Containerfrom aurelia-dependency-injection:
import Auth from './auth/storage';
import { Container } from 'aurelia-dependency-injection';
export function A(foo: boolean): boolean {
let auth = Container.instance.get(Auth);
if (auth.authorized && foo) {
return true
}
else {
return false
}
}
Run Code Online (Sandbox Code Playgroud)