1、在项目中下载依赖 npm install jquery --save-dev 当然用淘宝镜像 cnpm 也是可以的
注意:jquery 一定要小写 不然会提示 Please use ‘jquery’ (all lowercase)
安装完成后,在package.json里会自动生成jQuery的依赖。
2、找到build文件夹下的webpack.base.conf.js文件,打开,添加:
const webpack=require('webpack')
在module.exports里输入:
plugins: [
new webpack.optimize.CommonsChunkPlugin('common.js'),
new webpack.ProvidePlugin({
jQuery: "jquery",
$: "jquery",
"windows.jQuery":"jquery"
})
]
3、在入口文件main.js中输入:
import $ from 'jquery'
4、在app.vue中调用jquery
<script>
export default {
name: 'App',
mounted: function() {
console.log($);
console.log(jQuery);
console.log(windows.jQuery);
}
}
</script>