简体   繁体   中英

Aurelia dependency injection when instantiating objects

If I create a supporting class, eg UserList that has HttpClient injected it into it, then whoever instantiates that class will have to pass an HttpClient object to it in the constructor. Shouldn't the @inject(HttpClient) take care of getting the HttpClient singleton and injecting it into the constructor? Otherwise every class that needs to refererence UserList will also have get a reference to HttpClient so that it can then pass it to the UserList constructor (and defeating the purpose of injection).

UserList.ts

@inject(HttpClient)
export class UserList {
    constructor(public http: HttpClient){
    }
...
}

DoSomething.ts

export class DoSomething {
    userList: UserList;

    constructor(){
         this.userList = new UserList(); //doesn't work without passing HttpClient
    }
}

to make this work I have to get a reference to HttpClient in the DoSomething class even though it won't be using it directly. The working version which seems to be poorly implemented:

DoSomething.ts

@inject(HttpClient)
export class DoSomething {
    userList: UserList;

    constructor(public http: HttpClient){
         this.userList = new UserList(http); 
    }
}

If you use typescript, don't worry about this. Use @autoinject and see magic happen!

Like this:

import {autoinject} from 'aurelia-framework';

@autoinject()
export class UserList {
    constructor(private http: HttpClient){
    }
...
}

In other file:

import {autoinject} from 'aurelia-framework';

@autoinject()
export class DoSomething {
    constructor(private userList: UserList){
    }
}

The TypeScript compiler will emit the type metadata and Aurelia will read this injecting instances on correct way!

More information about: http://aurelia.io/docs.html#/aurelia/dependency-injection/1.0.0-beta.1.2.3/doc/article/dependency-injection-basics

The correct way to handle this is to use a Factory Resolver

import { Factory } from 'aurelia-framework';

@inject(Factory.of(UserList))
export class DoSomething {

    userList: UserList;

    constructor(UserList) {

        // this is a factory, so you call the function without new
        this.userList = UserList();
    }
}

@inject(HttpClient)
export class UserList {

    http: HttpClient;

    constructor(HttpClient) {
        this.http = HttpClient;
    }
}

For more information see the answer given in this related question , or the official docs .

You need to inject the UserList in DoSomething

import {UserList} from 'where-ever-user-list-is';
import {inject} from 'aurelia-framework';

@inject(UserList)
export class DoSomething {
    userList: UserList;

    constructor(userList){
         this.userList = userList
    }
}

If you want to use Aurelia dependency injection , you need to import the required modules:

import {HttpClient} from 'aurelia-fetch-client';
import {inject} from 'aurelia-framework';

@inject(HttpClient)
export class DoSomething{

    constructor(http){
        // do your stuff
    }

}

This is the ES6 implementation, the one I use, but I believe all you need to change is the type in the constructor .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM