Skip to content

Property injection doesn't work for services #16

Description

@ashleydavis

I've been making considerable progress with react.di, although I've just spent an hour to finally figure out that property injection for services doesn't seem to work.

Is there a way to enable this? If not already, do you plan to support it? It seems like a logical and convenient setup for a service.

Example of the problem follows...

I was trying to do this:

@Injectable
export class Commander implements ICommander {

    @Inject("ILog")
    log!: ILog;

    // ....
}

Then I was getting this error:

[15:21:23.904] [error] Error: Component "Commander" need to be nested in a Module or Provider Component to use dependency injection.
    at checkIfContainerExists (node_modules\react.di\lib\component-injection.js:30:15)

After I converted it to constructor injection I got it to work:

@Injectable
export class Commander implements ICommander {

    log!: ILog;

    constructor(
        @Inject("ILog")  // For some reason these can't be injected as properties with services!
        log: ILog
    ) {
        this.log = log;
    }

    // ...
}

Thanks!
Ash

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions