使用模块名称而不是文件名加载模块
我在两个单独的 js 文件中有两个“定义”。
def1.js 和 def2.js
define("mydefname1",["file1",...]});
define("mydefname2",["file2",....]});
我有另一个 require 语句,我检查这两个定义是否已加载。
require(['def1','def2'], function(){alert('loaded')});
这工作正常..
但如果我尝试
require(['mydefname1','mydefname2'], function(){alert('loaded')});,
它就不起作用。
有没有办法我实际上可以使用 mydefname1 和 mydefname2.. 即模块名称来加载它们,而不是文件名?
I have two 'define' in two separate js files.
def1.js and def2.js
define("mydefname1",["file1",...]});
define("mydefname2",["file2",....]});
I have another require statement where i check if the two definetions are loaded.
require(['def1','def2'], function(){alert('loaded')});
this works fine..
but if I try
require(['mydefname1','mydefname2'], function(){alert('loaded')});,
it does not work.
Is there a way I could actually use mydefname1 and mydefname2.. i.e. the module name to load them, and not the file name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,这是不可能的。我通常做的就是格式化我的文件名以包含我的模块名称,这样我就可以这样包含它。
对于元类名称 Overlay,文件名将为 class.Overlay.js,我的 require/include 函数将采用我的模块名称并从中构建文件名。
As far as I know, that's not possible. What I usually do is format my file names to include my module name, so I can include it that way.
For a metaclass name Overlay, the file name would be class.Overlay.js and my require/include functions would take my module name and build the file name from it.
据我了解,RequireJS 建议您避免手动分配模块名称 - 请参阅此处: http://requirejs .org/docs/api.html#modulename
FWIW,您是否使用过“path”配置选项?我认为您不能用它来完全完成您想要做的事情,但它可能提供一种可接受的替代方法。请参阅此处:http://requirejs.org/docs/api.html#config
希望有帮助!
As I understand it, RequireJS recommends that you avoid manually assigning module names - see here: http://requirejs.org/docs/api.html#modulename
FWIW, have you played around with the "path" configuration option? I don't think you can use it to do exactly what you are looking to do but it may offer an acceptable alternate approach. See here: http://requirejs.org/docs/api.html#config
Hope it helps!