Aurelia-注入功能
Aurelia- Inject into function
在我的 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
}
}
我知道我可以将它包装在 class 中并使用它,但想知道是否有任何方法可以实现类似于此
如果要在函数中使用依赖注入,请使用 Container
from 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
}
}
在我的 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
}
}
我知道我可以将它包装在 class 中并使用它,但想知道是否有任何方法可以实现类似于此
如果要在函数中使用依赖注入,请使用 Container
from 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
}
}