Home > Web Front-end > Front-end Q&A > Are vue and jquery compatible?

Are vue and jquery compatible?

青灯夜游
Release: 2021-11-19 13:51:55
Original
5061 people have browsed it

vue is compatible with jquery. Reasonable use of JQuery and Vue will not cause conflict because they have different focuses. Vue focuses on data binding and view components, while JQuery focuses on asynchronous requests and animation effects; and JQuery and Vue can complete asynchronous tasks very efficiently when working together.

Are vue and jquery compatible?

The operating environment of this tutorial: windows7 system, jquery1.10.2&&vue2.9.6 version, Dell G3 computer.

The reasonable use of JQuery and VueJS will not cause conflict because they have different focuses. VueJS focuses on data binding and view components, and JQuery focuses on asynchronous requests and animation effects. If you use JQuery for VueJS development, you must process all HTML components through JQuery after Vue has rendered them. When using JQuery, you should avoid directly manipulating the DOM, but applying animation is allowed.

JQuery and VueJS can work together to complete asynchronous tasks very efficiently. First, an Ajax request is issued through JQuery. After receiving the JSON data passed by the server, the data is then bound to the component through Vue. Finally, JQuery When performing animation processing, the whole process is as natural as flowing clouds and flowing water.

How to use jquery in the vue project

Assuming that you have used vue-cli to build the development scaffolding, next, look at the following .

1. NPM install jQuery, run the following code in the project root directory

npm install jquery --save
Copy after login

2. Webpack configuration

Find webpack.base in the build directory in the project root directory. conf.js file, use the following code at the beginning to introduce webpack, because this file is not referenced by default.

var webpack = require('webpack')
Copy after login

Then add a piece of code in module.exports,

// 原有代码resolve: {
 extensions: ['.js', '.vue', '.json'],
 alias: {'vue$': 'vue/dist/vue.esm.js','@': resolve('src')
 }
},// 添加代码plugins: [
 new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
jquery: "jquery","window.jQuery": "jquery"
 })
],// 原有代码module: {
 rules: [// ...... ]
}
Copy after login

Then many other solutions say that importing it in main.js is enough, but the questioner did so .

Import jQuery in main.js

import 'jquery'
Copy after login

Use $ or jQuery in the Vue component to write the code to operate the dom

Then start the project

npm run dev
Copy after login

But the compilation reported an error:

http://eslint.org/docs/rules/no-undef '$' is not defined or
http://eslint.org/docs/rules/no-undef 'jQuery' is not defined
Copy after login

What's going on? ? ?

3. eslint check

Smart friends must have thought that it is related to eslint. Yes, the next step you need to do at this time is to modify the .eslintrc.js file in the root directory. After modifying In the module.exports of the file, just add a key-value pair jquery: true for env, that is:

env: { // 原有
 browser: true, // 添加
 jquery: true
 }
Copy after login

Againnpm run dev, OK, no error is reported, go quickly Try it in the component, console.log($('selector')), you will find that you can successfully use jQuery to obtain the DOM.

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of Are vue and jquery compatible?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template