javascript 使用 require.js 加载非 amd 模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13168816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading non amd modules with require.js
提问by Lawrence
Currently I am using require.js for a fun side project I am working everything is working fine except a code syntax higlighting plugin called prism.js. I can see that the plugin is being pulled via the network tab in chrome, but the plugin isn't initializing.
目前我正在将 require.js 用于一个有趣的副项目我正在工作,除了一个名为prism.js 的代码语法高亮插件外,一切正常。我可以看到插件是通过 chrome 中的网络选项卡拉出的,但插件没有初始化。
I am not sure if it's a require problem or uf the plugin is the issue and was wondering if anyone could help.
我不确定这是一个需要的问题还是插件是问题所在,我想知道是否有人可以提供帮助。
Here is a look at my main.js:
看看我的 main.js:
require.config({
// 3rd party script alias names
paths: {
// Core Libraries
modernizr: "libs/modernizr",
jquery: "libs/jquery",
underscore: "libs/lodash",
backbone: "libs/backbone",
handlebars: "libs/handlebars",
text: "libs/text",
prism: "plugins/prism",
templates: "../templates"
},
// Sets the configuration for your third party scripts that are not AMD compatible
shim: {
"backbone": {
"deps": ["underscore", "jquery", "handlebars"],
"exports": "Backbone" //attaches "Backbone" to the window object
}
}
});
// Include Specific JavaScript
require(['prism', 'modernizr', 'jquery', 'backbone', 'routers/router', 'views/AppVIew' ],
function(Prism, Modernizr, $, Backbone, Router, App) {
this.router = new Router();
this.App = new App();
}
);
回答by Chris Salzberg
Change the shim section to include prism, and make sure it exports "Prism":
更改垫片部分以包含棱镜,并确保它导出“棱镜”:
shim: {
"backbone": {
"deps": ["underscore", "jquery", "handlebars"],
"exports": "Backbone" //attaches "Backbone" to the window object
},
"prism": {
"exports": "Prism"
}
}
回答by Gokhan Tank
Handlebars and Prism are not compatible with AMD(Asyncronous Module Definition) so you need to shimit yourself like below;
Handlebars 和 Prism 与 AMD(Asyncronous Module Definition)不兼容,所以你需要像下面这样自己填充它;
requirejs.config({
shim: {
'backbone': {
"deps": ["underscore", "jquery", "handlebars"],
"exports": "Backbone" //attaches "Backbone" to the window object
},
'handlebars': {
"exports": 'Handlebars'
},
'prism': {
"exports": "Prism"
}
}
});
You may wish to look at the require.js shim documentation site; http://requirejs.org/docs/api.html#config-shim
您可能希望查看 require.js shim 文档站点; http://requirejs.org/docs/api.html#config-shim
Hope this will help
希望这会有所帮助
回答by yakxxx
Prism should be added to shim
too. Just as backbone it is not AMD compliant and therefore must be declared same way.
棱镜也应该加shim
进去。就像主干一样,它不符合 AMD 标准,因此必须以相同的方式声明。