mardi 11 avril 2017

How to use ES6 module in ember.js (with ember cli)?

Standard project set up with ember cli seems to be using ES6 modules when I generate controllers/routes/models etc. with the cli. Sometimes though I want to import/export an additional function/module ie. I may want to write a function that I use in the controller in a separate file. When I try to import the function in the standard ES6 way ember-cli seems to have a problem with handling it. Let's say I've created controller with:

ember g route tesit

then I create a function in app/routes/testit/logger.js

const logger = function(msg) {
  console.log(msg);
};

export default logger;

and import it in my controller:

import Ember from 'ember';
import logger from './testit/logger.js'

export default Ember.Route.extend({
  beforeModel() {
    logger('it works');
  }
});

then I get the following error:

Error: Could not find module myproject/routes/testit/logger.js imported from myproject/routes/testit

How can I resolve it?




Aucun commentaire:

Enregistrer un commentaire