0

我编写了一个自定义模块,例如它正在打印"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"

它应该来自本地主机的javascript文件......可能是什么问题?

4

1 回答 1

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!");
        }
    });

});
于 2012-09-28T16:27:53.220 回答