Component injection fails

I’ve got a simple initializer that injects a singleton class into views and components. When I use the component in a template like


I am then able to access the injected object from the component.

If I create an instance of the component from code:

comp = App.ComponentNameComponent.create()

the injected object is null.

Does injection only work on components added via a template? Is there something special I need to do to inject into components that I want to manage programmatically?

Ember’s DI system works through an application container. Creating a raw instance of a class will not result in an injection being made.

There is a guide on DI at the Ember website. Creating instances by hand should really not be needed- maybe you can provide more context about your use case.

Thanks, @mixonic, I think I got it figured out.

How about for objects loaded by es6 modules (ember cli)? Does creating a new instance like the following result in the injection being made?

import ComponentNameComponent from 'app/components/component-name';
comp = ComponentNameComponent.create();