简体   繁体   English

关闭 - 库未正确加载

[英]Closure - library not loading properly

I've got a Closure environment running fine in my development environment. 我的Closure环境在我的开发环境中运行良好。 In a new folder & file, however, it is not working. 但是,在新文件夹和文件中,它无法正常工作。

For example, take this test script: 例如,采用此测试脚本:

goog.require('goog.dom');
console.log(goog);
console.log(goog.dom);
goog.dom.getElement('logout').innerHTML = "Heck";

That's all, plus the base.js in the HTML. 这就是全部,加上HTML中的base.js

The first console.log shows an object with all the right stuff is in there. 第一个console.log显示一个对象,其中包含所有正确的东西。 All the files are loaded into the DOM too. 所有文件也都加载到DOM中。

The second console log, however, says undefined and so the last line doesn't work (obviously). 但是,第二个控制台日志表示undefined ,因此最后一行不起作用(显然)。 The Chrome console error is Uncaught TypeError: Cannot call method 'getElement' of undefined . Chrome控制台错误是Uncaught TypeError: Cannot call method 'getElement' of undefined

What the heck is going on? 到底他妈发生了什么? It's the same if I try XhrIo . 如果我尝试XhrIo It's not finding the methods on the goog object. 它没有在goog对象上找到方法。

Thanks. 谢谢。

Documentation: 文档:

Note: Do not put your goog.require() statements in the same script tag as the entry point to code that uses the goog.required functions or classes. 注意:不要将goog.require()语句放在与使用goog.required函数或类的代码的入口点相同的脚本标记中。 A goog.require() call adds code to the document after the script tag containing the call. goog.require()调用包含调用的脚本标记之后向代码添加代码。

So 所以

<script>
    goog.require('goog.dom');
    console.log(goog.dom);
</script>

prints undefined . 打印undefined But

<script>
    goog.require('goog.dom');
</script>
<script>
    console.log(goog.dom);
</script>

or 要么

<script>
    goog.require('goog.dom');
    addEventListener('load', function() {
        console.log(goog.dom);
    });
</script>

prints an Object. 打印一个对象。

FYI, there are other possible workflows. 仅供参考,还有其他可能的工作流程。 For example, the closurebuilder.py script can help to load all the required files ahead of time. 例如,closurebuilder.py脚本可以帮助提前加载所有必需的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM