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