将jQuer的文件导入到项目中,然后直接使用即可。
<script src="jQuery.js"></script>
- cnpm install jquery --save
-
- // 或者
- npm install jquery --save
我们想在哪个组件中使用jQuery库,首先要使用如下命令引入jquery,然后就可以正常使用了
import $ from 'jquery'
- <template>
- <div id="app">
-
- </div>
- </template>
-
- <script>
- import $ from 'jquery'
-
- export default {
- name: 'App',
- components: {},
- data: function () {
- return {}
- },
- created:function(){
- console.log($('#app'));
- }
- }
- </script>
-
- <style>
-
- </style>
若每一个组件中都去使用jquery,必须在每个组件中写:import $ from 'jquery';比较麻烦,所以使用全局变量
let webpack = require('webpack')

- plugins: [
- new webpack.ProvidePlugin({
- $: "jquery",
- jQuery: "jquery",
- jquery: "jquery",
- "window.jQuery": "jquery"
- })
- ],

修改根目录下.eslintrc.js 文件的env 节点,为env添加一个键值对 jquery: true 就可以了
- env: {
- // 原有
- browser: true,
- // 添加
- jquery: true
- }

不需要再import 引用 jQuery 了,可以 直接使用$了,
console.log($('选择器'))
你会发现成功使用jQuery获取到了DOM;或者按下面的,能看到jq是一个函数