我正在学习Requirejs,并且从两个简单的.html
页面开始:index.html
和second.html
。
在index.html
我的怀抱中:
<script data-main="assets/js/app.min" src="js/vendor/require.js"></script>
该app.min.js
文件如下所示:
requirejs.config({
baseUrl: 'js/vendor',
paths: {
app: '../app',
jquery: 'jquery-1.10.1.min'
}
});
requirejs(["app/main"]);
我的app/main.js
文件只有一个jQuery警报:
define(['jquery'], function($) {
$(function() {
alert('Hello World');
});
});
工作正常!现在,我担心的只是一件事...如果我需要app/main
为所有页面全局加载一个全局文件,然后又app/second
在second.html
页面上运行另一个类似的文件,那该怎么办?
可能我缺少有关Requirejs的内容...我认为我不需要app.min.js
像上一样将文件中的所有内容都加载到文件中app/main
。
我知道我可以在单独的js
文件中定义模块,但是如何在不将所有内容加载到一个文件中的情况下为不同的页面管理不同的文件呢?可能是我错了,希望您可以为此打开脑海。
谢谢
我了解页面除了中的内容外,可能还需要自己的代码app.min
。您可以执行以下操作:
<script data-main="assets/js/app.min" src="js/vendor/require.js"></script>
<script>
// You can call the config function as many times as you need to add new configuration.
requirejs.config({
// Presumably, baseUrl does not need to be changed.
// baseUrl: 'js/vendor',
paths: {
// additional paths you may need
}
});
// This loads the code proper to this page.
requirejs(["app/second"]);
</script>
如果app/second
依赖,请app/main
确保在app/second
的define
调用中列出了该依赖项。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句