![](/img/trans.png)
[英]Error: Bootstrap's JavaScript requires jQuery, using Webpack
[英]Webpack bundle - Bootstrap's JavaScript requires jQuery
我想要實現以下目標:
bundle
( 按此順序 ) jquery
, tether
和bootstrap.js
。 HTML
頁面中加載此包,在其下方加載其他頁面特定的腳本。 為實現這一點,我使用webpack 2
和CommonsChunkPlugin
。 這是我的配置。
對於我有的entries
:
const scriptsEntry = {
blog_index: SRC_DIR + "/js/pages/blog_index.js",
blog_about: SRC_DIR + "/js/pages/blog_about.js",
vendor: ["jquery", "tether", "bootstrap"]
};
在plugins
部分:
scriptsPlugins.push(
new webpack.optimize.CommonsChunkPlugin({
name: "vendor",
filename:"vendor.bundle.js",
minChunks: 2
}),
...
}));
在'index.html'里面我加載了包:
<script src="{{ url_for('static', filename='dist/js/vendor.bundle.js') + anti_cache_token }}"></script>
<script src="{{ url_for('static', filename='dist/js/home.js') + anti_cache_token }}"></script>
在blog_index.js
的源目錄blog_index.js
我使用了jquery
:
import $ from "jquery";
$(".home-click").click(function () {
alert("clicked from .home-click");
});
結果:
.home-click
alert box
按預期激發。 vendor.bundle.js
包含: jquery
, tether
和bootstrap
。 blog_index.js
(在通過webpack 2
運行之后),我注意到jquery
導入沒有捆綁在這個文件中,而是vendor.bundle.js
(這是預期的)。 我嘗試在這個行vendor: ["jquery", "tether", "bootstrap"]
切換庫的順序vendor: ["jquery", "tether", "bootstrap"]
,但沒有任何改變 - 錯誤仍然存在。
你知道我怎么能解決這個問題,最好不要使用額外的webpack
插件?
Bootstrap的javascript假設jQuery掛在window
對象上,它不需要它或任何東西。
通過捆綁東西,您不會將變量暴露給全局范圍,或者至少您不應該這樣做。 所以bootstrap代碼找不到jQuery。
將它添加到你的插件,你應該沒問題
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
tether: 'tether',
Tether: 'tether',
'window.Tether': 'tether',
})
這將替換jQuery
被假定為全局的所有實例,並導出jQuery包,即jQuery
對象。 Tether
相同
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.