如何在Vue3.0中引入jQuery并使用

本文介绍了如何在Vue项目中安装并配置jQuery,包括通过npm安装,创建vue.config.js文件配置ProvidePlugin,修改.eslintrc.js文件启用jQuery支持,以及在main.js中导入jQuery。最后展示了在组件中使用jQuery进行DOM操作的例子。
摘要由CSDN通过智能技术生成

1、安装jQuery
需要在项目根目录下使用 npm 命令:

npm install jquery --save

2、vue.config.js文件
在项目根目录下创建一个 vue.config.js 文件(如果已经存在无需创建),进行如下配置:

const webpack = require('webpack')
module.exports = {
  configureWebpack: {
    plugins: [
      new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        'windows.jQuery': 'jquery'
      })
    ]
  }
}

在这里插入图片描述
3、.eslintrc.js文件
在项目目录中找到 .eslintrc.js 文件,在 env 中配置 jquery:true
在这里插入图片描述
4、最后在 main.js 文件中导入 jQuery
在这里插入图片描述
至此完成 jQuery 的引入和配置,可以全局使用。

使用例子

<template>
  <div class="home">
    <div class="d"></div>
  </div>
</template>

<script>
export default {
  name: 'Home',
  date() {
    return {}
  },
  mounted() {
    this.text()
  },
  methods: {
    text() {
      $('.d').click(function () {
        alert('1')
        $(this).hide()
      })
    }
  }
}
</script>

<style lang="less" scoped>
.d {
  width: 100px;
  height: 100px;
  background-color: cadetblue;
}
</style>
  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值