我使用RequireJS来声明我在项目中需要的依赖项。我已经配置了RequireJS,以便加载jQuery。但是,由于某些原因,浏览器会产生未定义jQuery的错误(Bootstrap.js需要jQuery)。
<script type="text/javascript" src="./modules/requirejs/require.js" data-main="./js/main.js"></script>
<script src="js/bootstrap.js"></script>
是因为requireJS异步加载了依赖项,所以它实际上可能发生在加载bootstrap.js之后吗?
发布于 2016-01-04 19:38:34
if (typeof jQuery === 'undefined') {
throw new Error('Bootstrap\'s JavaScript requires jQuery')
}
引导仅对jQuery进行全局检查。当浏览器尝试加载引导时,jQuery可能还没有出现,因为RequireJS异步加载模块。
现在Bootstrap并不将自己导出为AMD模块。为了确保RequireJS能够加载引导带,并确保它在jQuery之后加载,您可以使用路径和希姆配置。paths
将模块名映射到模块文件,而shim
则告诉RequireJS它的依赖项。
paths: {
// We tell RequireJS that `bootstrap` module is this file
bootstrap: 'path/to/bootstrap.js'
},
shim: {
// We tell RequireJS that `bootstrap` will need `jquery` loaded first
bootstrap: {
deps: ['jquery'],
},
}
然后在您的main.js
中,将bootstrap
作为其依赖项之一加载。
https://stackoverflow.com/questions/34598262
复制相似问题