How to dynamically create a component


#1

I’m working on a module which needs to register components with Ember, Angular, React etc. depending on what is loaded into the page.

With React I can just do

if (window.React) {
  React.createClass(...

But how can I do the same with Ember? I’m looking for something like:

if (window.Ember) {
  Ember.registerComponent('my-component-name', Ember.Component.extend({
    ...
  }));
}

Is this possible?


#2

http://emberjs.com/api/classes/Ember.Application.html#method_register

app.register('component:x-foo', ComponentType);

There is also the component helper, and I’m unsure which you should use without knowing more about the usecase.

http://emberjs.com/api/classes/Ember.Templates.helpers.html#method_component


#3

Hmm. Is it possible to do this without having access to an app instance? In this case I’m hoping to register a component before any apps are even created.


#4

https://guides.emberjs.com/v2.0.0/applications/initializers/