Ember CLI - Error when using moment.js in route

血红的双手。 提交于 2019-12-11 00:39:22

问题


I have imported moment.js into my project and it seems to work just fine in my controllers but for some reason it is not working in my routes.

Controller:

// controllers/users.js
import Ember from 'ember';

export default Ember.Controller.extend({
    date: function() {
        alert(moment().format('X'));
    }.property()

    ...
});

Route:

// routes/users.js 
// (Error: /routes/users.js: line 5, col 29, 'moment' is not defined.
import Ember from 'ember';

export default Ember.Route.extend({
    model: function() {
        var data = { start: moment().startOf('month').startOf('day').format('X') };
        return this.store.find('event', data);
    }
});

Brocfile:

var app = new EmberApp();

app.import('vendor/moment/moment.js');

回答1:


I guess this is a JsHint Error. You may want to add the following comment to your Route code.

/* global moment:true */
import Ember from "ember";
....



回答2:


Also (from ember-cli documentation):

If you want to use external libraries that write to a global namespace (e.g. moment.js), you need to add those to the predef section of your project’s .jshintrc file and set its value to true. If you use the lib in tests, need to add it to your tests/.jshintrc file, too.

Then you don't have to do it to every file your using moment.js in.



来源:https://stackoverflow.com/questions/24771540/ember-cli-error-when-using-moment-js-in-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!