Home > Web Front-end > Front-end Q&A > How to introduce jq into vue

How to introduce jq into vue

王林
Release: 2023-05-18 09:09:07
Original
4880 people have browsed it

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.

  1. Install jQuery
    First, we need to install jQuery in the Vue project.
    You can enter the following command in the command line window:
npm install jquery --save
Copy after login

After the installation is completed, jQuery will be automatically added to the package.json file.

  1. Introducing jQuery
    Introducing jQuery into the Vue project, there are the following two ways:

(1)require method

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')
Copy after login

(2) Directly introduce

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>
Copy after login
  1. Using jQuery
    When jQuery is introduced into the Vue project, you can use it happily. Here is an example of using bootstrap's tooltip plug-in:
<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>
Copy after login

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!

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