我写的模块简目录
/index.html
/vendor/jquery.js
/vendor/hl.js
/vendor/require.js
/js/index.js
其中 hl扩展了jquery 的$.getUrlPrameter() 方法
index.js是 index.html的业务逻辑模块 index.js中需要调用$.getUrlPrameter()
(function(factory) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
factory(require("jquery"));
else if (typeof define == "function" && define.amd)// AMD
define(["jquery"], factory);
else
(this || window).HL = factory();
})(function($) {
$.getUrlParameter = function(parameterName){
//获取url参数值
var reg = new RegExp("(^|&)" + parameterName + "=([^&]*)(&|$)", "i");
var arr = location.search.substr(1).match(reg);
if (arr)
return arr[2];
else
return null;
}
})
define(function(require){
var $ = require('../vendor/jquery');
var HL = require('../vendor/hl');
function init(){
alert($.getUrlParameter('v'))
};
return {
init:init
}
})
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<script src="vendor/requirejs/require.js"></script>
<script>
require.config({
paths: {
"jquery": "vendor/jquery",
"hl":"vendor/hl"
}
});
require(['jquery','hl','./js/index'],function($,hl,m){
m.init();
})
</script>
</body>
</html>
请问我这错在哪里了?为何在index.js中没能初始化
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你的index.js里
你在config里的path配置过模块id了 导致两个两个名字指向一个文件
参考 这个错误的第三条
http://requirejs.org/docs/errors.html#timeout