Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aurelia- Inject into function

Tags:

aurelia

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

like image 471
Lewis Avatar asked Jan 04 '23 05:01

Lewis


1 Answers

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
    }
}
like image 133
Jeff G Avatar answered Mar 06 '23 19:03

Jeff G