javascript - RequireJs with moment.js 不触发 `init`

标签 javascript requirejs momentjs

我的 requirejs 配置声明 moment.js并且我想在需要库时设置默认语言环境。所以,我试过这个:

require.config({
  enforceDefine: true,
  noGlobal: true,
  baseUrl: '/js',
  shim: {
    'moment': {
      init: function (moment) {
        console.log("*** Moment loaded!");
        return moment;
      }
    }
  },
  paths: {
    'moment': 'lib/moment-with-locales.min'

  }
});

但是,调用 require(['moment'], function (moment) { ... }); 不会调用 中的 init 函数垫片配置。

我在这里没有得到什么?

最佳答案

Moment.js 将自己定义为 AMD 模块,RequireJS 不会调用 init

来自 RequireJS 的文档:

The init function will not be called for AMD modules. For example, you cannot use a shim init function to call jQuery's noConflict. See Mapping Modules to use noConflict for an alternate approach to jQuery.

来自 Moment.js 的主要来源:

https://github.com/moment/moment/blob/develop/moment.js#L2796

关于javascript - RequireJs with moment.js 不触发 `init`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25124367/

相关文章:

javascript - 使用 RequireJS 加载 Backbone 和 Underscore

javascript - 使用时区偏移获取时区名称/列表

javascript - 基于第一个 knockout 教程,为什么我的代码没有运行?

Javascript - 列出网络浏览器正在下载的文件

javascript - 在同一对象上添加第二个动画会删除 css 属性

javascript - 有没有办法从窗口获取 momentJS ?

javascript - Momentjs 我的格式有什么问题

javascript - 使用 Canvas 的 Buggy 行为?

javascript - Require JS 定义函数中如何处理参数

javascript - 在intellij中使用requirejs时JsTestDriver断言