vue 引入jquery

参考

https://www.npmjs.com/package/jquery

1. 因为已经安装了vue脚手架,所以需要在webpack中全局引入jquery

npm i jquery
安装失败可尝试
打开package.json文件,在里面加入这行代码,jquery后面的是版本,根据你自己需求更改。

dependencies:{
  "jquery":"^2.2.1"
}

npm install
确保node_moduel下 有jquery

2.build/webpack.base.conf.js文件修改

'use strict'
var webpack=require("webpack")
...


module.exports = {
 ...
  plugins: [
    new webpack.optimize.CommonsChunkPlugin('common.js'),
    new webpack.ProvidePlugin({
      jQuery: "jquery",
      $: "jquery"
    })
  ]
}

在文件头部加入 require
module.exports引入plugins

3、在main.js中引入,加入下面这行代码,全局引入

import $ from 'jquery'

4、测试使用 在vue组件文件

   created () {
      $('document').ready(() => {
        console.log('1111')
        console.log('jquery', $)
        console.log('jquery', $('.hello').attr('class'))
      })

你可能感兴趣的:(vue 引入jquery)