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"
}
接下来就能正常使用了。