码迷,mamicode.com
首页 > Web开发 > 详细

vue 中使用jquery

时间:2018-07-30 22:10:02      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:plugins   resolve   文件中   npm   ide   错误   jquery   asc   OLE   

1.安装jquery

npm install jquery --save

2.webpack配置

在项目根目录下的build目录下找到webpack.base.conf.js文件,在开头使用以下代码引入webpack,因为该文件默认没有引用,

var webpack = require(‘webpack‘)

然后在module.exports中添加一段代码,

// 原有代码
resolve: {

},
// 添加代码

plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
})
],
// 原有代码
module: {
rules: [
// ......
]
}

3.在main.js中导入jquery

import ‘jquery‘

4.在.vue文件中就可以操作dom了 console.log($(‘选择器‘)) 然后执行npm run dev

注意:如果你的项目装了eslint,可能会报一下的错误

但是编译却报错了:

http://eslint.org/docs/rules/no-undef ‘$‘ is not defined or
http://eslint.org/docs/rules/no-undef ‘jQuery‘ is not defined

这时候需要做的下一步就是要修改根目录下.eslintrc.js文件了,在改文件的module.exports中,为env添加一个键值对 jquery: true
env: {
// 原有
browser: true,
// 添加
jquery: true
}
再次执行npm run dev 就可以啦

 

 

 

 

vue 中使用jquery

标签:plugins   resolve   文件中   npm   ide   错误   jquery   asc   OLE   

原文地址:https://www.cnblogs.com/zousaili/p/9392996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!