美文网首页
如何在Vue中引入jquery

如何在Vue中引入jquery

作者: 为梦齐舞 | 来源:发表于2020-05-29 14:54 被阅读0次

1、使用npm安装jquery

npm install jquery

2、在工程中添加vue.config.js文件,写入如下代码

const webpack = require('webpack')
module.exports = {
    publicPath: './',
    configureWebpack: {

        plugins: [
 
           new webpack.ProvidePlugin({
 
             $:"jquery",
 
             jQuery:"jquery",
 
             "windows.jQuery":"jquery"
 
           })
 
         ]
 
     }
}

3、在main.js中加入引用

import $ from "jquery";

4、如果vue中安装了eslint,可能出现如下报错:“'$' is defined but never used”,
可以在package.json中的eslintConfig选项下rules节点中,添加如下代码

"rules": {
      "no-unused-vars":"off"
    }

接下来就能正常使用了。

相关文章