gpt4 book ai didi

Aurelia-注入(inject)函数

转载 作者:行者123 更新时间:2023-12-04 22:55:52 29 4
gpt4 key购买 nike

在我的 Aurelia SPA 中,我有一些我想在不同模块中使用的功能。它依赖于调用时给出的参数和单例的参数。有没有办法创建一个导出函数,我可以将我的 Auth 单例注入(inject)其中,而不必在每次调用该函数时将其作为参数传递?
我希望它看起来如何的一个简单示例就是这个。

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
}
}

我知道我可以将它包装在一个类中并使用它,但想知道是否有任何方法可以实现类似于此

最佳答案

如果要在函数中使用依赖注入(inject),请使用 Container来自 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-注入(inject)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47222216/

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