通过requirejs中的插件停止全局jquery [英] Stop global jquery with plugin in requirejs
问题描述
我有 jquery
和 jquery-cookie
在下方正确协同工作。我想让 jquery
不是全局的。现在是。我尝试过的任何方法似乎都打破了 jquery-cookie
。
I have jquery
and jquery-cookie
working together correctly below. I would like to make is so that jquery
isn't global. Right now it is. Any method i've tried seems to break jquery-cookie
.
require.config({
paths: {
"jquery": '../components/jquery/jquery',
"jquery-cookie": "../components/jquery-cookie/jquery.cookie",
},
shim: {
"jquery-cookie": {
deps: ['jquery']
}
}
});
require(["jquery", "jquery-cookie"], function($) {
console.log($().jquery); // 1.7.2
console.log($.cookie("hello")); // null
});
以下更改将使jquery local和break cookie成为可能:
The following changes will make jquery local and break cookie:
define("nc-jquery",['jquery'], function () {
return jQuery.noConflict(true);
});
require(["nc-jquery", "jquery-cookie"], function($) {
console.log($().jquery);
console.log($.cookie("hello"));
});
推荐答案
我发现这个工作,我只是不认为这是最好的解决方案。
I found that this works, I just don't think it's the best solution.
require(["jquery", "jquery-cookie"], function() {
var $ = jQuery.noConflict(true);
console.log($().jquery);
console.log($.cookie("hello"));
});
我对这段代码的解释以及它的工作原理如下。 jquery-cookie
在代码中使用 $
,并使用 $ $ c $只要足够的时间来加载jquery和cookie,c>是全局的。我使用noConflict本地化,允许
$
在此需求中使用。
My explanation of this code and why it works is as follows. jquery-cookie
uses the $
in it's code and with this $
is global for just enough time for both jquery and cookie to load. I make it local with noConflict allowing $
usage within this require.
这篇关于通过requirejs中的插件停止全局jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!