VueCLI3.0中使用jQuery和Bootstrap的方法

這篇文章主要介紹了Vue CLI3.0中使用jQuery和Bootstrap的方法,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

十年專注成都網(wǎng)站制作,成都企業(yè)網(wǎng)站建設,個人網(wǎng)站制作服務,為大家分享網(wǎng)站制作知識、方案,網(wǎng)站設計流程、步驟,成功服務上千家企業(yè)。為您提供網(wǎng)站建設,網(wǎng)站制作,網(wǎng)頁設計及定制高端網(wǎng)站建設服務,專注于成都企業(yè)網(wǎng)站建設,高端網(wǎng)頁制作,對葡萄架等多個領域,擁有多年建站經(jīng)驗。

Vue 中使用 jQuery 和 Bootstrap 不是特別符合 Vue 原生的寫法,但是有時候又要用,所以放上我的引入設置,供大家參考。

在 Vue CLI2.0 中引入 jQuery 和 Bootstrap 需要設置很多配置項,網(wǎng)上有很多方法法,這里不重復寫了。直接上 Vue CLI3.0 配置步驟。

第一步:安裝  jQuery、 Bootstrap、popper.js依賴。

其中popper.js 用于在 Bootstrap 中顯示彈窗、提示、下拉菜單,所以需要引入。

npm install jquery bootstrap@3 popper.js --save

注意:上面的 bootstrap@3 指的是安裝 Bootstrap 第三版,如果不加 @3 符號,默認安裝第四版。

第二步:配置 main.js

引入 Boostrap 請看配置文件。

//main.js

import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
//在這里引入 bootstrap。默認只引入 bootstrap 中的 js,css 需要另外引入,我的 bootstrap.ss 在APP.vue中引入的
import "bootstrap";
//也可以在這里引入 bootstrap.css ;
//import "bootstrap/dist/css/bootstrap.css";

Vue.config.productionTip = false;

new Vue({
  router: router,
  store: store,
  render: h => h(App)
}).$mount("#app");

我的 APP.vue 的配置,只是引入 bootstrap.css,代碼僅供參考。

<style>
// 因為我的 bootstrap 文件經(jīng)過了我自己的調(diào)整,所以單獨放在 assets 文件夾中做單獨引入。
//如果你只是想使用原生的 bootstrap,直接在 main.js 中引入 css 文件即可。
@import "./assets/css/bootstrap.css";
</style>

第三步:配置 vue.config.js 文件

Vue CLI3.0 中的所有配置都在 vue.config.js 文件,你在這里配置好,腳手架自動使用你的配置覆蓋掉默認的配置。

如果你的項目中沒有 vue.config.js 文件,請你在 package.json 文件的同級目錄新建一個 vue.config.js 文件。文件內(nèi)具體的配置如下:

const webpack = require("webpack");

module.exports = {
//configureWebpack 是Vue CLI3.0 中用于配置 webpack 插件參數(shù)的地方,你在這里設置,會新建或者覆蓋 webpack 默認配置。
//webpack ProvidePlugin 的含義是創(chuàng)建一個全局的變量,使這個變量在 webpack 各個模塊內(nèi)都可以使用。這里的配置含義是創(chuàng)建 '$'、'jQuery'、'window.jQuery' 三個變量指向 jquery 依賴,創(chuàng)建 'Popper' 變量指向 popper.js 依賴。
    configureWebpack: {
        plugins: [
            new webpack.ProvidePlugin({
                $: 'jquery',
                jQuery: 'jquery',
                'window.jQuery': 'jquery',
                Popper: ['popper.js', 'default']
              })
        ]
      }
}

第四步:具體使用范例

我做了一個 tooltip 的示例,鼠標放上去會出現(xiàn) tooltip 提示

//template
<template>
  <div class="content-wrap">
    <button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="left" title="Tooltip on left">Tooltip on left</button>
    <button type="button" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="Tooltip on top">Tooltip on top</button>
    <button type="button" class="btn btn-warning" data-toggle="tooltip" data-placement="bottom" title="Tooltip on bottom">Tooltip on bottom</button>
    <button type="button" class="btn btn-danger" data-toggle="tooltip" data-placement="right" title="Tooltip on right">Tooltip on right</button>
  </div>
</template>


<script>
export default {
  name: "componentsTooltips",
  mounted: function() {
    //在頁面加載完畢后初始化 tooltip, 相當于$(function(){ $('[data-toggle="tooltip"]').tooltip(); }
    $('[data-toggle="tooltip"]').tooltip();
  }
};
</script>

如果 eslint 報誤,請設置 .eslintrc.js 文件。

module.exports = {
  env: {
    node: true,
    jquery: true
  }
};

本人測試結果如下:

Vue CLI3.0中使用jQuery和Bootstrap的方法

感謝你能夠認真閱讀完這篇文章,希望小編分享Vue CLI3.0中使用jQuery和Bootstrap的方法內(nèi)容對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細的解決方法等著你來學習!

當前名稱:VueCLI3.0中使用jQuery和Bootstrap的方法
文章URL:http://bm7419.com/article30/goscpo.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設、微信公眾號、微信小程序網(wǎng)站排名、網(wǎng)頁設計公司、手機網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設