vue-cli安裝的vue項目中引用jquery


1. 安裝jquery

  npm install jquery --save

2. 或是在package.json指定jq版本號后,運行npm install命令,安裝指定版本的jquery

  "dependencies": {
    "core-js": "^3.3.2",
    "jquery": "^3.4.1",
    "qrcodejs2": "0.0.2",
    "vue": "^2.6.10",
    "vue-router": "^3.1.3",
    "vuex": "^3.0.1"
  },

3. 根目錄下新建vue.config.js,並配置

const webpack = require('webpack');

module.exports = {
    configureWebpack: {//引入jquery
        plugins: [
            new webpack.ProvidePlugin({
                $: "jquery",
                jQuery: "jquery",
                "windows.jQuery": "jquery"
            })
        ]
    },
    
}

這樣,便可在vue項目中使用jquery了。

說到這里也順便提一下,我們設置代理也是在vue.config.js中添加的,代碼如下

const webpack = require('webpack');

module.exports = {
    configureWebpack: {//引入jquery
        plugins: [
            new webpack.ProvidePlugin({
                $: "jquery",
                jQuery: "jquery",
                "windows.jQuery": "jquery"
            })
        ]
    },
    devServer: {
        port: 8089,
        proxy: "http://localhost:8080",
        // proxy:{
        //   '/': {
        //     target: 'http://localhost:8080/',
        //     changeOrigin: true,
        //     pathRewrite: {}
        //   },
        // }
    },
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM