作者:僾媙 | 来源:互联网 | 2023-05-26 20:57
IamtryingtowriteaRequireJSmodulethatdependsonjQueryandajQueryplugin.Iamhavingtrou
I am trying to write a RequireJS module that depends on jQuery and a jQuery plugin. I am having trouble to declare these dependencies on module level & settings up the shim config – maybe I missed something, but this how I am trying it:
我正在尝试编写一个依赖于jQuery和jQuery插件的RequireJS模块。我无法在模块级别声明这些依赖关系并设置shim配置 - 也许我错过了一些东西,但这是我如何尝试它:
In my application config, I set the path to my module
在我的应用程序配置中,我设置了我的模块的路径
requirejs.config({
paths: {
// loads modules/myModule.js
myModule: 'modules/myModule',
}
});
require(['app']);
Which should now be available in app.js
:
哪个应该在app.js中可用:
define(['myModule'], function (mm) {
// Do stuff with myModule, now available as mm here
});
But how do declare the dependencies for my module – jQuery and the plugin?
但是如何声明我的模块的依赖项 - jQuery和插件?
I found this wonderful wiki entry where multiple possibilities are shown, but I don't see how that could fit into my module?
我找到了这个精彩的wiki条目,其中显示了多种可能性,但我看不出它如何适合我的模块?
I already got this defined in a separate file:
我已经在一个单独的文件中定义了这个:
var require = {
paths: {
jquery: 'path/to/jquery',
plugin: 'path/to/plugin'
},
shim: {
plugin: {
deps: ['jquery']
}
}
};
From the docs:
来自文档:
Also, you can define the config object as the global variable require before require.js is loaded, and have the values applied automatically. This example specifies some dependencies to load as soon as require.js defines require():
此外,您可以在加载require.js之前将配置对象定义为全局变量require,并自动应用值。此示例指定在require.js定义require()时立即加载的一些依赖项:
But where should I put this configuration? Including it before require.js
seems a bit contra-productive – if there are 10 modules with dependencies, then I would need to include 10 separate config files?
但是我应该把这个配置放在哪里?在require.js之前包括它似乎有点反作用 - 如果有10个模块具有依赖关系,那么我需要包含10个单独的配置文件?
1 个解决方案