我编写了一个自定义的modyle,例如它正在向控制台打印"Hello World"
。
require([
"dojo/_base/declare"
],
function(declare) {
return [
declare("Hello", null, {
printHello : function() {
console.log("Hello, World!");
}
})
];
}
);
.js
文件的名称为“Hello.js
”。在我的html页面中,我需要这个模块,但是我在加载它时遇到了问题。我的代码:
<script type="text/javascript">
var dojoConfig = {
async : true,
parseOnLoad : false,
isDebug : true,
packages: [
{ name: "gui", location: "/scripts/gui" }
]
};
require([
"gui/common/Hello"
],
function(HelloFunciton) {
var hello = new Hello();
hello.printHello();
});
</script>
但我在控制台中出错:
"NetworkError: 404 Not Found - http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/scripts/gui/common/Hello.js"
它应该来自localhost的javascript文件... 可能是什么问题?
答案 0 :(得分:1)
将baseUrl: "./"
添加到您的dojoConfig
。
还可以通过./scripts/gui
建立相对于您的包的路径。
完整的HTML文件:
<html>
<head>
<script>
var dojoConfig = {
async: true,
baseUrl: "./",
parseOnLoad: false,
isDebug: true,
packages: [
{ name: "gui", location: "./scripts/gui" }
]
}
</script>
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js"></script>
<script>
require(["gui/common/Hello"], function(Hello) {
var hello = new Hello();
hello.printHello();
});
</script>
</head>
<body>
</body>
</html>
模块文件./scripts/gui/common/Hello.js
:
define([
"dojo/_base/declare"
], function(
declare
) {
return declare([], {
printHello: function() {
console.log("Hello world!");
}
});
});