本文介绍了Requirejs 使用 shim的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通过requirejs2使用shim,有没有办法告诉requires模块已经加载?
Using shim by requirejs2, there is a way to tell to requires that a module is already loaded?
示例:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="undescrore.js"></script>
<script type="text/javascript" src="require.js'"></script>
<script type="text/javascript">
require.config({
paths: {
"backbone": '/vendor/js/backbone-min.js'
},
shim: {
'backbone': {
//These script dependencies should be loaded before loading
//backbone.js
deps: ['underscore', 'jquery'], // here I would like to load the already loaded library
}
}
});
</script>
推荐答案
好吧,如果下划线已经加载并且可用,那么您根本不需要垫片.Backbone 将愉快地加载.如果没有,可能是因为下划线没有实际加载.
Well, if underscore is already loaded and available, you do not need the shim at all. Backbone will happily load. If not, it's probably because underscore is not actually loaded.
虽然仅部分使用 require.js 听起来是错误的,但您不妨 AMD 加载它们.为此,您需要像这样更改垫片:
It sounds however wrong to be only partially using require.js, you might as well AMD-load them all. To do that you will need to change your shim like this:
shim: {
backbone: {
deps: ["underscore", "jquery"],
exports: "Backbone"
},
underscore: {
exports: "_"
}
}
当然还要更新您的路径
.
这篇关于Requirejs 使用 shim的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!