javascript dojo 教程:dojo 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14598719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:03:39  来源:igfitidea点击:

dojo tutorial: dojo is not defined

javascripthtmldojo

提问by RCK69

I want to get startet with dojo.

我想开始使用道场。

Therefore I am useing their tutorials: http://dojotoolkit.org/documentation/tutorials/1.8/hello_dojo/

因此我使用他们的教程:http: //dojotoolkit.org/documentation/tutorials/1.8/hello_dojo/

The simplest tutorial displays this page

最简单的教程显示这个页面

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Tutorial: Hello Dojo!</title>
</head>
<body>
    <h1 id="greeting">Hello</h1>
    <!-- load Dojo -->
    <script src="//ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js"
               data-dojo-config="async: true"></script>
</body>
</html>

I now open the page (tried both localy and hosted version on their page). And when I write

我现在打开页面(在他们的页面上尝试了本地和托管版本)。当我写

dojo.query("h1")

in my firebug console I get the message:

在我的萤火虫控制台中,我收到以下消息:

ReferenceError: dojo is not defined

参考错误:未定义 dojo

Please help

请帮忙

采纳答案by user1875420

are your sure your source for dojo is in "//ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js" because your folder structure look like in googleapis folder which is "http://ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js"

您确定您的 dojo 源在“//ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js”中,因为您的文件夹结构在 googleapis 文件夹中看起来像“ http:// ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js"

回答by g00glen00b

This question is lacking a correct answer. The reason this is not working is because you enabled asyncmode. What this actually does is that the Dojo core will be loaded asynchronously.

这个问题缺乏正确答案。这不起作用的原因是因为您启用了async模式。这实际上是将异步加载 Dojo 核心。

The Dojo coreis the part of Dojo that is loaded automatically when you load the dojo.jsfile. It sets a global variable called dojowhich contains basic functionality like the dojo.querypart.

道场核心是,当你加载时自动加载道场的部分dojo.js文件。它设置了一个名为的全局变量dojo,其中包含像dojo.query零件这样的基本功能。

Your problem is that you're actually not waiting for the core to load. Because the core is not loaded, dojowill be undefined, giving you that error.

你的问题是你实际上不是在等待核心加载。因为核心没有加载,dojo将会是undefined,给你那个错误。



You should only use asyncmode when using the AMD loader (require()), if you don't want to use it (legacy mode), you just put asyncto false. But this mode is actually deprecatedand will be removed in Dojo 2.0.

您应该只使用async采用了AMD装载机(当模式require()),如果你不想使用它(传统模式),你只要把asyncfalse。但这种模式实际上弃用,并将在 Dojo 2.0 中移除。

The other solution is to use the AMD loader (asynchronous module loader), the proper syntax to this is:

另一种解决方案是使用 AMD 加载器(异步模块加载器),正确的语法是:

require([ "dojo/query" ], function(query) {
    query("h1");
});

In this example you might have the chance that the DOM is not loaded yet, so the best answer is to wait for the DOM to load as well, resulting in:

在这个例子中,你可能有机会还没有加载 DOM,所以最好的答案是等待 DOM 加载,结果:

require([ "dojo/query", "dojo/domReady!" ], function(query) {
    query("h1");
});


You're indicating that it's working when you use the protocol implied URL. However, this is not true. The only reason why it suddenly is working is because you left the asyncproperty away, which defaults to false.

当您使用协议隐含 URL 时,您表示它正在工作。然而,事实并非如此。它突然起作用的唯一原因是因为您离开了该async属性,默认为false

Unlike Christofersaid, the legacy mode is still available, but it's deprecated.

Christofer所说的不同,传统模式仍然可用,但已被弃用。

Agnes' answerwill work because because it's using the AMD loader. However, combining legacy code and the new syntax doesn't look well. If you're choosing for AMD you should put everything in AMD and not only certain parts.

Agnes 的答案会起作用,因为它使用的是 AMD 加载程序。但是,将遗留代码和新语法结合起来看起来不太好。如果您选择 AMD,您应该将所有内容都放在 AMD 中,而不仅仅是某些部分。

回答by Christofer Eliasson

Having no previous experience of Dojo, I read through a bit of the documentation. Especially this part, talking about the "Modern Dojo".

由于之前没有使用过 Dojo,我通读了一些文档。尤其是这部分,讲的是“现代道场”。

It turns out, as of version 1.7, you can no longer just load dojo.js and expect to call dojo.something. With the "new Dojo", that is no longer possible. This is why you get dojo is not defined.

事实证明,从 1.7 版开始,您不能再仅仅加载 dojo.js 并期望调用dojo.something. 有了“新道场”,这不再可能。这就是为什么你得到dojo is not defined.

For more info, read through the updated documentation on how to get started, but here is a simple hello world:

有关更多信息,请阅读有关如何开始的更新文档,但这里有一个简单的 hello world:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Tutorial: Hello Dojo!</title>
    <link rel="stylesheet" href="../../../resources/style/demo.css">
</head>
<body>
    <h1 id="greeting">Hello</h1>
    <!-- load dojo and provide config via data attribute -->
        <script src="//ajax.googleapis.com/ajax/libs/dojo/1.8.3/dojo/dojo.js" data-dojo-config="isDebug:1, async:1"></script>
    <script>
        require(["dojo/dom", "dojo/domReady!"], function(dom){
            var greeting = dom.byId("greeting");
            greeting.innerHTML += " from Dojo!";
        });
    </script>
</body>
</html>

If you like to use the old way, I guess you could reference a version of Dojo prior to 1.7, but using a legacy version is rarely a good way forward, so I recommend that you learn the new way of doing things instead.

如果您喜欢使用旧方式,我想您可以参考 1.7 之前的 Dojo 版本,但使用旧版本很少是前进的好方法,因此我建议您改为学习新的做事方式。

回答by Agnes Marcinik

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Title</title>
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.9.1/dojo/dojo.js" data-dojo-   config="async: true"></script>
</head>

<body>

<script>
require(["dojo"], function(dojo){
dojo.ready(function(){  
//Your code here
});
});
</script>

</body>
</html>