In my Aurelia SPA I have some functions I want to use in different modules. It relys on parameters given when called and parameters of a singleton. Is there a way to create an export function that I can inject my Auth singleton into without having to pass it as a parameter everytime I call the function? A simple example of how I'd like it to look would be this.
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
}
}
I know I could just wrap it in a class and use that but want to know if there is any way to achieve it similar to this
If you want to use dependency injection in a function, use 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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With