While creating components on ember. Skipping .js file?

Viewed 1143

If i run below, comment for generating component in ember.

$ ember generate component my-new<br>
installing component<br>
  create app\components\my-new.hbs<br>
  **skip app/components/my-new.js<br>**
  tip to add a class, run ember generate component-class my-new <br>
installing component-test <br>
  create tests\integration\components\my-new-test.js<br>

It doesn't create both componentName.js and template files. How to solve it?

3 Answers

Yep, it's the default behavior in Ember since Glimmer template-only components were introduced. To fix it you should add one of --component-class options, e.g.

ember generate component my-new --component-class @glimmer/component

or, use an alias ember generate component my-new -gc.

The full list of options available could be retrieved by calling ember help g

The answer is in the output

 tip to add a class, run ember generate component-class my-new <br>

In your terminal, wherever you just typed ember generate component my-new, type ember generate component-class my-new

This will create the backing my-new.js file for your component.

In Ember Octane, a component doesn't require a backing .js file anymore so the default generator does not create one.

Alternatively, you could manually create the my-new.js file in your components directory with touch or copying an existing component. I can't tell you exactly where the file would go since you may / may not be colocating, using pods, etc.

Use either

ember generate component -gc my-component

to generate a glimmer component or

ember generate component -cc my-component

However notice that you *not always need a .js file. A component with just an .hbs is totally valid and even more performant (if you dont actually need the .js).

Related