gpt4 book ai didi

javascript - 要求 js 从 cdn 加载脚本失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:45:56 25 4
gpt4 key购买 nike

我是 RequireJS 的新手,出于某种原因我无法通过 CDN 加载脚本。

我的代码:

// site full url
var siteUrl = window.location.protocol+"//"+window.location.host + "/fresh/";

// requirejs config
requirejs.config({
baseUrl: siteUrl + "assets/js/",
paths: {
"plugins": "plugins",
"scripts": "scripts",
"jquery": "https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min",
"jquery-ui": "https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min",
"bootstrap": "https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min",
}
});

require(['jquery', 'jquery-ui', 'bootstrap', 'plugins/chosen'], function($, chosen){

/*
loading global selectors and variables
*/

//chosen for select boxes
$(".chzn-select").chosen();

});

jquery 加载失败。我收到以下错误:

ReferenceError: jQuery is not defined
[Megszakítás ennél a hibánál]

...h"):this.container.removeClass("chzn-container-single-nosearch")),e="",s=this.re...

chosen.js (1. sor)

ReferenceError: jQuery is not defined


TypeError: $(...).chosen is not a function
$(".chzn-select").chosen();

有人可以指出我做错了什么吗?

P.S:我正在定义站点 URL,因为我使用的是 Laravel,如果没有该定义,它会在基本 URL 中包含 URL 段。

最佳答案

Brandon 是正确的,而我认为 shim 不能解决问题

来自 RequireJS Doc :

Do not mix CDN loading with shim config in a build. Example scenario: you load jQuery from the CDN but use the shim config to load something like the stock version of Backbone that depends on jQuery. When you do the build, be sure to inline jQuery in the built file and do not load it from the CDN. Otherwise, Backbone will be inlined in the built file and it will execute before the CDN-loaded jQuery will load. This is because the shim config just delays loading of the files until dependencies are loaded, but does not do any auto-wrapping of define. After a build, the dependencies are already inlined, the shim config cannot delay execution of the non-define()'d code until later. define()'d modules do work with CDN loaded code after a build because they properly wrap their source in define factory function that will not execute until dependencies are loaded. So the lesson: shim config is a stop-gap measure for non-modular code, legacy code. define()'d modules are better.

简而言之,我相信当您拥有不是 requirejs 的模块并且它们依赖于其他一些模块 (jQuery) 时,您将无法在 RequireJS 中使用来自 CDN 的 jQuery。

在这种情况下,我同意 Brandon 的建议,直接在页面上要求它们可能会更好。

关于javascript - 要求 js 从 cdn 加载脚本失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14751996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com