我试过了
http://chapter31.com/2006/12/07/including-js-files-from-within-js-files/
How do I include a JavaScript file in another JavaScript file?
https://github.com/volojs/create-template/blob/master/www/lib/require.js
我想创建像这样的东西
function include(file) {
var script = document.createElement ("script");
script.src = file;
script.type = "text/javascript";
script.defer = true;
script.async = false;
var head = document.getElementsByTagName("head");
head.item(0).appendChild(script);
}
// Include some JS
include("../grains/vect3.js");
include("../grains/particle.js");
include("../grains/sphere.js");
include("../grains/grid4.js");
include("../grains/field.js");
include("../grains/force.js");
include("../grains/point2.js");
include("../color/rgb.js");
include("../canvas/graphics.js");
include("../data/dataxy.js");
include("../canvas/transform.js");
include("../canvas/xychart.js");
include("../test/test.js");
include("../canvas/chart2.js");
include("../canvas/xyseries.js");
但它不起作用,因为一些脚本依赖于其他脚本。
这个想法是让头脑中的东西像
<script>
require(
[
"https://raw.githack.com/dudung/butiran/master/grains/particle.js",
"https://raw.githack.com/dudung/butiran/master/grains/vect3.js",
],
butiranTest
);
</script>
并在体内
<script>
butiranTest();
function butiranTest() {
var p = new Particle();
console.log(p);
}
</script>
其中类Particle依赖于Vect3类。
最后,它解决了
// Set base url
var baseurl = "https://rawgit.com/dudung/butiran/master/";
// Set to false when not download whole scripts
var local = true;
if(local) {
baseurl = "";
}
// Define libraries of butiran.js
var libs = [
baseurl + "chart/chart2.js",
baseurl + "chart/xyseries.js",
baseurl + "color/rgb.js",
baseurl + "demo/demo.js",
baseurl + "demo/exam.js",
baseurl + "demo/mjlaplaceplate.js",
baseurl + "framework/generator.js",
baseurl + "framework/industry.js",
baseurl + "framework/resource.js",
baseurl + "grains/grid4.js",
baseurl + "grains/particle.js",
baseurl + "grains/sphere.js",
baseurl + "grains/vect3.js",
baseurl + "math/integration.js",
baseurl + "math/polynomial.js",
baseurl + "math/random.js",
baseurl + "mathjax/update.js",
baseurl + "timer/timer.js",
baseurl + "visual/electronics/capacitors.js",
baseurl + "visual/electronics/connectors.js",
baseurl + "visual/electronics/point.js",
baseurl + "visual/coordinates.js",
baseurl + "visual/painter.js",
baseurl + "z/test_capacitors.js",
baseurl + "z/test_demo.js",
baseurl + "z/test_generator.js",
baseurl + "z/test_generator_chart2.js",
baseurl + "z/test_integration.js",
baseurl + "z/test_random.js",
baseurl + "z/test_timer.js",
];
require(libs, main);
// Define main script using butiran.js
function main() {
var eout = document.createElement("div");
eout.id = "scriptResult";
document.body.appendChild(eout);
examThreeGrains();
}
在没有互联网连接的情况下进行开发时,变量local
设置为true
。
答案 0 :(得分:0)
使用require.js非常简单。假设您有一些脚本/some/script.js
,它依赖于/some/script2.js
,例如jquery。 /some/script.js
中的require.js代码如下所示:
require(['/path/to/jquery.js', '/some/script2.js'], function () {
// Callback function which executes after the dependencies are resolved
});
您可以根据所需的依赖关系和时间来实现多个require调用。还要求js知道如果依赖关系已经解决,它将不会尝试从服务器获取文件并再次解析。