1、引入jquery
jQuery 直接在 html 中引入,然后在 webpack 中把它配置為全局即可。
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
<img src="src/img/ais.jpg"/>
<div id="app">
</div>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
</body>
</html>
webpack.config.js進行配置:
//將外部變量或者模塊加載進來
externals : { 'jquery' : 'window.jQuery' }
有時我們希望我們通過script引入的庫,如用CDN的方式引入的jquery,我們在使用時,依舊用require的方式來使用,但是卻不希望webpack將它又編譯進文件中。
// 我們不想這么用 // const $ = window.jQuery // 而是這么用 const $ = require("jquery") $("#content").html("<h1>hello world</h1>")
https://www.cnblogs.com/samli15999/p/7047968.html
2、jquery插件
import './css/common.css'; import layer from './components/layer/layer.js'; const App = function(){ var dom= document.getElementById('app'); var Layer = new layer(); dom.innerHTML = Layer.tpl; } //引入插件 require('./js/jquery.tabSwitch.js'); //使用插件 $('.p').tabSwitch({tabName: '.aa',tabContent: 'ul'}); new App()
如果要全局引用jQuery
,不管Query插件有沒有支持模塊化,用externals
就對了。