在webpack中引入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>")

猜你喜欢

转载自blog.csdn.net/luoxiping1/article/details/80507918