Home >Web Front-end >Front-end Q&A >How to introduce jq into vue
With the continuous development of front-end development, there are now more and more types of front-end technology frameworks, such as React, Vue, Angular, etc. Among them, Vue is currently one of the most popular front-end frameworks. It has the advantages of powerful component-based development, convenient template syntax, and good rendering performance. In addition, Vue can also easily introduce jQuery and make better use of jQuery plug-ins and methods to achieve richer effects.
So, how to introduce and use jQuery in Vue? Here is a brief introduction.
npm install jquery --save
After the installation is completed, jQuery will be automatically added to the package.json file.
Can be used in main Use require in .js to introduce jQuery:
import Vue from 'vue' import App from './App.vue' Vue.config.productionTip = false window.jQuery = require('jquery') require('bootstrap/dist/js/bootstrap.min') new Vue({ render: h => h(App), }).$mount('#app')
in index.html. You can also directly introduce jQuery in index.html:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue App</title> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap/4.5.2/js/bootstrap.min.js"></script> </head> <body> <div id="app"></div> </body> </html>
<template> <div class="container"> <button type="button" class="btn btn-primary mt-5" data-toggle="tooltip" title="这是一段提示文本"> Hover over me </button> </div> </template> <script> export default { mounted() { //在mounted方法中初始化tooltip插件 $('[data-toggle="tooltip"]').tooltip() } } </script>
It should be noted here that operating DOM elements in a Vue project also requires the use of jQuery's $ symbol. Don't forget to introduce $ when using it.
At this point, we can easily introduce jQuery into the Vue project and use it. After such operations, we can better combine Vue's component development and rich jQuery plug-ins and methods, bringing more possibilities to project development and optimization, making front-end development more convenient.
The above is the detailed content of How to introduce jq into vue. For more information, please follow other related articles on the PHP Chinese website!