我的 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'snoConflict
. 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/