简体   繁体   English

如何从CDN goog.require JQuery?

[英]How to goog.require JQuery from CDN?

I am currently experimenting with different JavaScript module formats and loader and also wanted to try out Google Closure . 我目前正在尝试使用不同的JavaScript模块格式和加载程序,还想尝试使用Google Closure

I've got the basic example working, but can't figure out how to use goog.require for an external library. 我已经有了基本示例 ,但无法弄清楚如何将goog.require用于外部库。 Let's take JQuery from CDN , for example. 让我们以CDN中JQuery为例。

Right now I'm doing the following. 现在,我正在执行以下操作。 In the HTML page: 在HTML页面中:

<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>

And in the script: 并在脚本中:

goog.require("Greets");
goog.provide("Greets.MsgDivMessenger");

Greets.MsgDivMessenger = function() {};
Greets.MsgDivMessenger.prototype.sendMessage = function(message)
{
    $('#msg').text(message);
};

This works as the global $ is included via the <script .../> tag in the HTML page itself. 这是因为HTML本身中的<script .../>标记包含了全局$

But what I'd like to do is to load JQuery via goog.require , without the <script .../> tag in the HTML page: 但是我想做的是通过goog.require加载JQuery, 在HTML页面中没有 <script .../>标记:

goog.require("Greets");
goog.require("$");
goog.provide("Greets.MsgDivMessenger");

Greets.MsgDivMessenger = function() {};
Greets.MsgDivMessenger.prototype.sendMessage = function(message)
{
    $('#msg').text(message);
};

I have tried adding JQuery CDN URL as dependency: 我尝试将JQuery CDN URL添加为依赖项:

goog.addDependency('https://code.jquery.com/jquery-2.1.3.min.js', ['$'], []);

But this did not work, goog.require has tried to load .../closure/goog/https://code.jquery.com/jquery-2.1.3.min.js instead. 但这没有用, goog.require尝试加载.../closure/goog/https://code.jquery.com/jquery-2.1.3.min.js代替。

Yes, I know that I don't need JQuery for $('msg') , but the point is to manage modules (including external ones) with Google Closure. 是的,我知道我不需要$('msg') JQuery,但重点是要使用Google Closure管理模块(包括外部模块)。

How could I goog.require JQuery from CDN? 如何从CDN goog.require JQuery?

This isn't the intended use and there is no support for it. 这不是预期的用途,对此不提供任何支持。 goog.provide/require is a dependency management scheme. goog.provide/require是一种依赖项管理方案。 A file that declares a "goog.provide", can be required by declaring it with a goog.require . 可以使用goog.require声明一个声明为“ goog.provide”的goog.require For production use, the dependencies would be preloaded or bundled. 对于生产用途,依赖项将被预加载或捆绑。

Unless you want to hack Closure, your only option is to host it with your sources. 除非您想破解Closure,否则唯一的选择就是将其与源一起托管。

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

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