2019-05-07 vue中使用bootstrap,able時ui預(yù)設(shè)樣式失效

今天在做項目的時候遇到一些坑:

如何在vue中使用bootstrap,并且在使用bootstrap的table時ui預(yù)設(shè)樣式失效,使用letter-spacing屬性后text-align:center;屬性失效。

第一步:安裝 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ù)的地方,你在這里設(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
<script>
export default {
  name: "componentsTooltips",
  mounted: function() {
    //在頁面加載完畢后初始化 tooltip, 相當于$(function(){ $('[data-toggle="tooltip"]').tooltip(); }
    $('[data-toggle="tooltip"]').tooltip();
  }
};
</script>

如果 eslint 報誤,請設(shè)置 .eslintrc.js 文件。

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

本人測試結(jié)果如下:


13170229-be5fab2566be49b0.jpg

第五步:使用過程中發(fā)現(xiàn)template中的bootstrap對table的預(yù)設(shè)css失效

<template>
  <div class="content-wrap">
    <table class="table table-hover table-bordered">
        <tr>
          <td>1-1</td>
          <td>1-2</td>
          <td>1-3</td>
          <td>1-4</td>
          <td>1-5</td>
        </tr>
        <tr>
          <td>2-1</td>
          <td>2-2</td>
          <td>2-3</td>
          <td>2-4</td>
          <td>2-5</td>
        </tr>
        <tr>
          <td>3-1</td>
          <td>3-2</td>
          <td>3-3</td>
          <td>3-4</td>
          <td>3-5</td>
        </tr>
    </table>
  </div>
</template>

可以看動table中的td沒有成功的添加border樣式


TIM圖片20190507141809.png

結(jié)果發(fā)現(xiàn)問題在于table與tr之前忽略了tbody標簽導(dǎo)致 th同理

<template>
  <div class="content-wrap">
    <table class="table table-hover table-bordered">
      <tbody>
        <tr>
          <td>1-1</td>
          <td>1-2</td>
          <td>1-3</td>
          <td>1-4</td>
          <td>1-5</td>
        </tr>
        <tr>
          <td>2-1</td>
          <td>2-2</td>
          <td>2-3</td>
          <td>2-4</td>
          <td>2-5</td>
        </tr>
        <tr>
          <td>3-1</td>
          <td>3-2</td>
          <td>3-3</td>
          <td>3-4</td>
          <td>3-5</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

效果如下:


TIM圖片20190507143428.png

第六步:好的,下面說一下letter:spacing的css的問題

TIM圖片20190507163815.png

圖中可以看到在使用了letter:spacing之后text-align屬性失效了。原因在于letter:spacing屬性給每個字的右側(cè)增加了一段距離,也就是你設(shè)置的屬性值。這與text-align對文字的布局沖突了,解決辦法的話也比較簡單:
TIM圖片20190507164136.png

在文字前邊加上text-indent屬性,屬性值與letter:spacing的屬性值一致即可。
參考鏈接:http://www.itdecent.cn/p/0d0c1eaeb877

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 引言 我們重視用戶的隱私。您在使用我們的服務(wù)時,我們可能會收集和使用您的相關(guān)信息。我們希望通過本《隱私政策》向您說...
    cadProject閱讀 367評論 0 0
  • 1. 需求的出現(xiàn) 由于我要做的是混合整數(shù)規(guī)劃問題(MIP),考慮到有現(xiàn)成的求解工具,于是決定使用Cplex來進行計...
    AJohn11閱讀 19,859評論 5 2
  • 參考文章:http://www.itdecent.cn/p/fd399ad4b7d8 1、安裝vue-cli 2...
    飛天宵夜閱讀 627評論 0 1
  • 只有經(jīng)過一定的量變,才會引起質(zhì)變,否則張嘴大白話,提筆寫不出; 只有厚積才能薄發(fā),煙火或者彩虹雖然短暫,但那只是對...
    cc08閱讀 308評論 0 0
  • 【致謝】 阿德勒個體心理學(xué)中有一個重要的概念就是,人是屬于社會的人,人具有社會屬性與集體屬性。真誠的致謝與感激不但...
    昆山飛米粒心理與生涯閱讀 234評論 0 0

友情鏈接更多精彩內(nèi)容