RobinBuschmann / react.di

Dependency injection for react based upon inversify.
97 stars 5 forks source link

Property injection doesn't work for services #16

Open ashleydavis opened 6 years ago

ashleydavis commented 6 years ago

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