我正在学习Javascript和Node,我正在尝试创建一个扩展String的模块。首先我得到的错误需求没有定义,所以我开始使用requireJS。然后我得到了这个错误NS_ERROR_DOM_BAD_URI: Access to restricted URI denied
,所以我将project.html
移到与我的.js
文件相同的文件夹中。现在我得到的模块没有在require.js
中定义,我似乎无法弄清楚为什么。我已阅读了其他一些帖子,但我没有找到解决方案。我的文件结构看起来像这样模块未定义
- 条/
- 脚本/
- main.js
- require.js
- project.html
- 帮手/
- extendString.js
main.js
define(function(require, exports, module){
var StringHelperModule = require("helper/extendString.js");
StringHelperModule.extendString(String);
var tmp = 'Hello World'.strip(' ');
document.write(tmp);
//Outputs: HelloWorld
});
extendString.js
'use strict';
module.exports = function extendString(String){
String.prototype.strip = function (delimiter) {
return this.replace(delimiter, '');
};
};
个project.html
<!DOCTYPE html>
<html>
<head>
<script data-main='main' src='require.js'></script>
</head>
</html>
require.js
(function() {
// Separate function to avoid eval pollution, same with arguments use.
function exec() {
eval(arguments[0]); //This is line the error points to
}
require.load = function (context, moduleName, url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
exec(xhr.responseText);
//Support anonymous modules.
context.completeLoad(moduleName);
}
};
};
}());
我也越来越没有很好地形成,它说如果有任何与此
您是否试过:require(“./ helper/extendString.js”);'? (注意开头的点) –
@ leo.fcx是的,它没有帮助。我刚刚在requirejs网站上发现了一个可能是我的问题的常见错误,但我不知道如何解决它。 [error](http://requirejs.org/docs/errors.html#defineerror) – SirParselot