基於Vue的前端框架有很多,Element算一個,而BootstrapVue也可以非常不錯的一個,畢竟Bootstrap也是CSS中的大佬級別的,它和Vue的整合,使得開發起來更加方便了。BootstrapVue 是基於 Bootstrap v4 + Vue.js 的前端 UI 框架。它是流行的 Bootstrap 框架與 Vue.js 的集成。這個包稱為 BootstrapVue。它允許我們使用與 Bootstrap(v4)集成的自定義組件。
使用 BootstrapVue,任何人都可以從 Vanilla.js 或 jQuery 切換到 Vue.js,而無需擔心 Bootstrap 對 jQuery 的嚴重依賴,甚至無法找到解決方法。這就是 BootstrapVue 的救援方式。它有助於彌補這一差距,並允許 Vue 開發人員能夠輕松地在他們的項目中使用 Bootstrap。BootstrapVue不依賴Jquery。
1、BootstrapVue的安裝使用
我們假設你已經有Vue的項目環境,那么BootstrapVue的安裝使用介紹就很容易了,直接使用npm安裝即可。
npm install bootstrap-vue bootstrap
上面的命令將會安裝BootstrapVue和Bootstrap包。 BoostrapVue包中包含所有BootstrapVue組件,常規Bootstrap包含CSS文件。
接下來,讓我們設置剛剛安裝的BootstrapVue包。轉到你的main.js文件並將這行代碼添加到合適的位置,另外還需要將Bootstrap CSS文件導入到項目中。
import BootstrapVue from 'bootstrap-vue' Vue.use(BootstrapVue) import 'bootstrap/dist/css/bootstrap.css' import 'bootstrap-vue/dist/bootstrap-vue.css'
那么一般簡單的main.js文件內容如下所示。
//src/main.js import Vue from 'vue' import App from './App.vue' import BootstrapVue from 'bootstrap-vue' import 'bootstrap/dist/css/bootstrap.css' import 'bootstrap-vue/dist/bootstrap-vue.css' Vue.use(BootstrapVue) Vue.config.productionTip = false new Vue({ render: h => h(App), }).$mount('#app')
如果我們項目中使用了其他組件模塊,那么這些可能會有所不同。
2、BootstrapVue的組件使用
學習一項新東西,我們一般先了解一下相關的文檔。
GitHub庫的地址:https://github.com/topics/bootstrapvue
BootstrapVue的官網地址(可能受限無法訪問):https://bootstrap-vue.js.org/
BootstrapVue的中文網站地址如下: https://code.z01.com/bootstrap-vue/
通過在Vue項目中引入對應的 BootstrapVue,那么它的相關組件使用就參考官網的介紹了解即可。BootstrapVue中有很多和Bootstrap一樣的組件,不過標簽前綴需要加上b-
例如對於常用的按鈕界面代碼處理,如下所示。
<div> <b-button>Button</b-button> <b-button variant="danger">Button</b-button> <b-button variant="success">Button</b-button> <b-button variant="outline-primary">Button</b-button> </div>
界面如下所示,很有Bootstrap的風格!我們可以看到原先Bootstrap上的html的button加多了一個前綴b-,變為了b-button了。
卡片Card控件使用代碼如下所示
<div> <b-card title="Card Title" img-src="https://picsum.photos/600/300/?image=25" img-alt="Image" img-top tag="article" style="max-width: 20rem;" class="mb-2" > <b-card-text> Some quick example text to build on the card title and make up the bulk of the card's content. </b-card-text> <b-button href="#" variant="primary">Go somewhere</b-button> </b-card> </div>
其中類class中的mb-2就是邊距的定義,參考說明如下所示。
另外可能還有接觸到 p-2,pt-2,py-2,px-2 等類似的定義,后面小節再行說明。
另外Flex的布局也需了解下。
<div class="bg-light mb-3"> <div class="d-flex justify-content-start bg-secondary mb-3"> <div class="p-2">Flex item 1</div> <div class="p-2">Flex item 2</div> <div class="p-2">Flex item 3</div> </div> <div class="d-flex justify-content-end bg-secondary mb-3"> <div class="p-2">Flex item 1</div> <div class="p-2">Flex item 2</div> <div class="p-2">Flex item 3</div> </div> <div class="d-flex justify-content-center bg-secondary mb-3"> <div class="p-2">Flex item 1</div> <div class="p-2">Flex item 2</div> <div class="p-2">Flex item 3</div> </div> <div class="d-flex justify-content-between bg-secondary mb-3"> <div class="p-2">Flex item 1</div> <div class="p-2">Flex item 2</div> <div class="p-2">Flex item 3</div> </div> <div class="d-flex justify-content-around bg-light mb-3"> <div class="p-2">Flex item 1</div> <div class="p-2">Flex item 2</div> <div class="p-2">Flex item 3</div> </div> </div>
界面效果如下所示。
我們來一個展示柵格的例子,顯示卡片中圖片,文字等信息。
<b-container> <div v-if="list.length"> <b-row> <template v-for="data in list"> <b-col sm="4" v-bind:key="data.index"> <b-card v-bind:title="data.strCategory" v-bind:img-src="data.strCategoryThumb" img-alt="Image" img-top tag="article" style="max-width: 20rem;" class="mb-2"> <b-card-text>{{ `${data.strCategoryDescription.slice(0,100)}...` }}</b-card-text> <b-button href="#" variant="primary">View food</b-button> </b-card> </b-col> </template> </b-row> </div> <div v-else> <h5>No meals available yet 😢</h5> </div> </b-container>
整體界面效果如下所示
3、BootstrapVue的相關介紹
BootstrapVue的很多概念還是和Bootstrap的類似,畢竟Bootstrap的CSS已經是標准的了。不過我們需要了解相關的布局、顏色、類定義等信息,以確認他們之間的一些差異。
主題色彩
Bootstrap v4.4 SCSS中定義的默認顏色如下,所有主題顏色將自動作為所有BootstrapVue組件的color 變量提供。
顏色變量
組件Size屬性
間距處理
影響元素之間的間距是可以通過style的margin或padding屬性來實現,但這兩個屬性本意並不相同;margin影響的是本元素與相鄰外界元素之間的距離,這里簡稱外邊距;padding影響的元素本身與其內部子元素之間的距離,簡稱為內填充。
bootstrap4提供了簡寫的class名,名稱分別以m-開頭和p-開頭的類。
一、影響距離大小的值有
0,1,2,3,4,5,auto
(1)、margin值有
class名 |
等價的style |
m-0 |
等價於{margin:0 !important} |
m-1 |
等價於{margin:0.25rem !important} |
m-2 |
等價於{margin:0.5rem !important} |
m-3 |
等價於{margin:1rem !important} |
m-4 |
等價於{margin:1.5rem !important} |
m-5 |
等價於{margin:3rem !important} |
m-auto |
等價於{margin:auto !important} |
(2)、padding值有
class名 |
等價的style |
p-0 |
等價於{padding:0 !important} |
p-1 |
等價於{padding:0.25rem !important} |
p-2 |
等價於{padding:0.5rem !important} |
p-3 |
等價於{padding:1rem !important} |
p-4 |
等價於{padding:1.5rem !important} |
p-5 |
等價於{padding:3rem !important} |
p-auto |
等價於{padding:auto !important} |
二、調整某一側的邊距
有幾個縮寫,t,b,l,r,x,y含義分別是top,bottom,left,right,left和right,top和bottom
(1)margin例子,距離大小可以0-5與auto,這里只用期中一個值來說明含義
class名 |
等價的style |
mt-2 |
{margin-top: 0.5rem !important} |
mb-2 |
{margin-bottom: 0.5rem !important} |
ml-2 |
{margin-left: 0.5rem !important} |
mr-2 |
{margin-right: 0.5rem !important} |
mx-2 |
{margin-right: 0.5rem !important;margin-left: 0.5rem !important} |
my-2 |
{margin-top: 0.5rem !important;margin-bottom: 0.5rem !important} |
(2)padding例子
class名 |
等價的style |
pt-2 |
{padding-top: 0.5rem !important} |
pb-2 |
{padding-bottom: 0.5rem !important} |
pl-2 |
{padding-left: 0.5rem !important} |
pr-2 |
{padding-right: 0.5rem !important} |
px-2 |
{padding-right: 0.5rem !important;margin-left: 0.5rem !important} |
py-2 |
{padding-top: 0.5rem !important;margin-bottom: 0.5rem !important} |