Javascript 将moment.js与lang文件和require.js一起使用

Javascript 将moment.js与lang文件和require.js一起使用,javascript,requirejs,momentjs,chaplinjs,mvc,Javascript,Requirejs,Momentjs,Chaplinjs,Mvc,我目前正在尝试将moment.js库与require.js一起使用,但我仍然无法理解这样一个项目的正确设置。以下是我在main.js文件中的操作: requirejs.config({ baseUrl: 'app', paths: { // ... more parameters (all Backbone related) 'moment': 'lib/moment', 'moment_de':

我目前正在尝试将moment.js库与require.js一起使用,但我仍然无法理解这样一个项目的正确设置。以下是我在main.js文件中的操作:

requirejs.config({
    baseUrl: 'app',

        paths: {
            // ... more parameters (all Backbone related)
            'moment': 'lib/moment',
            'moment_de': 'lib/lang/de',
        },

    shim: {
        'moment' : {
            deps: [],
        },

        'moment_de': {
            deps: ['moment'],
        },

        // ... more parameters (all Backbone related)
    }
});
我使用一个单独的模块进行配置。该模块如下所示:

define(['moment', 'moment_de'], function(moment, de) {

    moment.lang('de');

    var configuration = {}
    // ...    
    return configuration;
});
require.config({
  config: {
    'moment': {
      noGlobal: true
    }
  },
  paths: {
    ...
    'moment': '../vendor/moment/moment',
    'moment_de': '../vendor/moment/locale/de',
    'moment_pl': '../vendor/moment/locale/pl'
    ...
  },
  ...
});
define([
  'chaplin'
  'lib/moment'
], function(Chaplin, moment) {
  'use strict';

  var MyView = Chaplin.View.extend({

    ...

    parse: function() {
      ...
      console.log(moment().format('LLL'));
      ...
    }

    ...

  });

  return MyView;
});
如您所见,我试图更改此文件中矩对象的全局语言,但遇到以下错误消息:

Uncaught Error: Module name "../moment" has not been loaded yet for context: _. Use require([])
后来:

Uncaught TypeError: Cannot call method 'preparse' of undefined 
第一条错误消息是正在加载的语言模块,尽管它应该在片刻模块之后加载(如果我做得对的话)。第二个是从尝试切换到尚未加载的语言模块的矩模块

有人能解释一下这个问题吗。提前谢谢

编辑:我使用简化的语言版本(例如)修复了该问题。显然,缩小版使用的是AMD格式,这使得在require.js项目中更容易包含

不过,我仍然不太明白为什么不能使用shim配置包含这些语言。也许有人能解释一下

require({
    paths: {
        'moment': '//cdnjs.cloudflare.com/ajax/libs/moment.js/2.0.0/moment.min',
        'moment_de': '//cdnjs.cloudflare.com/ajax/libs/moment.js/2.0.0/lang/de.min'
    }
}, ['moment', 'moment_de'], function(moment){
    moment.lang('de');
    alert(moment(1316116057189).fromNow());
});
您不需要填充模块,因为moment.js支持AMD。

另一种解决方案(2015年): 本例旨在演示如何将
moment.js
翻译与
navigator.language
属性一起使用,该属性通常是用户的首选语言

在配置中分别定义
moment.js
和语言文件,如下所示:

define(['moment', 'moment_de'], function(moment, de) {

    moment.lang('de');

    var configuration = {}
    // ...    
    return configuration;
});
require.config({
  config: {
    'moment': {
      noGlobal: true
    }
  },
  paths: {
    ...
    'moment': '../vendor/moment/moment',
    'moment_de': '../vendor/moment/locale/de',
    'moment_pl': '../vendor/moment/locale/pl'
    ...
  },
  ...
});
define([
  'chaplin'
  'lib/moment'
], function(Chaplin, moment) {
  'use strict';

  var MyView = Chaplin.View.extend({

    ...

    parse: function() {
      ...
      console.log(moment().format('LLL'));
      ...
    }

    ...

  });

  return MyView;
});
创建一个小模块,如
lib/moment.js
,并指定您的语言配置(您可以找到RFC4646语言标记的列表):

请注意:
moment.js
默认支持英语


在视图类(或任何其他类/普通脚本等)中,按如下方式使用:

define(['moment', 'moment_de'], function(moment, de) {

    moment.lang('de');

    var configuration = {}
    // ...    
    return configuration;
});
require.config({
  config: {
    'moment': {
      noGlobal: true
    }
  },
  paths: {
    ...
    'moment': '../vendor/moment/moment',
    'moment_de': '../vendor/moment/locale/de',
    'moment_pl': '../vendor/moment/locale/pl'
    ...
  },
  ...
});
define([
  'chaplin'
  'lib/moment'
], function(Chaplin, moment) {
  'use strict';

  var MyView = Chaplin.View.extend({

    ...

    parse: function() {
      ...
      console.log(moment().format('LLL'));
      ...
    }

    ...

  });

  return MyView;
});

未缩小的文件似乎差异很大,不能用作垫片。非精简版本使用的
require(“../moment”)…
似乎与RequireJS不兼容。未解析相对模块名称。请将解决方案作为答案发布,以便进行标记和投票…请注意,在本例中,如果时刻取决于时刻,则可能会出现问题。数组(如)中异步加载的模块。所以,很可能力矩_de在力矩之前加载,并且你们会有关于力矩未定义的周期性错误。我不确定,但我认为这就是填隙片的用途(或者这就是问题示例中使用填隙片的想法,但它不起作用)。因此,应始终在加载之前加载。垫片用于不支持AMD的库。