![](/img/trans.png)
[英]How to use require-js on node.js without having to require(“requirejs”) and redefine requirejs in each file?
[英]How to use JS require() without Node.js
这可能是一个蹩脚的问题,但是如何在常规JavaScript中实现与require()
(Node.js)相同的功能?
真的很感激一些帮助。
有几种服务可以让你这样做。
最受欢迎的是Browserify 。
基本上,它需要通过语法树读取文件并将其转换为类似于RequireJS的样式。
请注意,这需要额外的编译步骤。 (我们最终会在ES6中获得模块,所以那就是:))
http://requirejs.org/docs/start.html
名为RequireJS的模块加载器存在于浏览器内使用,无需使用Node.js或Rhino。
要使用它,请下载并保存脚本并将其包含在您的页面中
<!DOCTYPE html>
<html>
<head>
<title>My Sample Project</title>
<!-- data-main attribute tells require.js to load
scripts/main.js after require.js loads. -->
<script data-main="scripts/main" src="scripts/require.js"></script>
</head>
<body>
<h1>My Sample Project</h1>
</body>
</html>
data-main属性将指向您的主脚本,您可以使用以下命令加载其余脚本:
require(["helper/util"], function(util) {
//This function is called when scripts/helper/util.js is loaded.
//If util.js calls define(), then this function is not fired until
//util's dependencies have loaded, and the util argument will hold
//the module value for "helper/util".
});
查看http://requirejs.org上的文档以获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.