用Vue封裝導(dǎo)航欄組件
前言:把一個(gè)功能模塊使用組件化的思想充分封裝,如導(dǎo)航欄,這無(wú)論對(duì)我們的開發(fā)思想還是效率都有許多好處,在開發(fā)中,我們要盡量多得運(yùn)用組件化的開發(fā)思想,不要把所有代碼都寫在同一個(gè).vue文件中,這樣能大大提高代碼的可讀性。
封裝導(dǎo)航欄

主要思路:把紅色的部分當(dāng)成一個(gè)個(gè)組件,而他們只是圖片和文字不同,所以我們可以把他們封裝成同一個(gè)組件,然后向組件里傳入圖片信息和文字信息即可(可以用插槽)。
//TabBarItem.vue
<template>
<div class="tabBarItem" @click="tabBarItemClick">
<div v-if="!isActive">
<slot name="item-icon"></slot>
</div>
<div v-else>
<slot name="item-icon-active"></slot>
</div>
<div :style="isActiveColor">
<slot name="item-text"></slot>
</div>
</div>
</template>
<script>
export default {
name:"TabBarItem",
props:{
path:String,
activeColor:{
type:String,
default:"pink"
}
},
computed:{
isActive:{
get(){
return this.$route.path.indexOf(this.path)!==-1;
},
set(){}
},
isActiveColor(){
return this.isActive?{color:this.activeColor}:{}
}
},
methods:{
tabBarItemClick(){
this.$router.push(this.path);
}
}
}
</script>
<style scoped>
.tabBarItem{
flex: 1;
font-size: 12px;
}
.tabBarItem img{
margin-top: 3px;
width: 24px;
padding-bottom:3px ;
}
</style>
接下來(lái)就是封裝一個(gè)把這4個(gè)選項(xiàng)放在同一個(gè)地方的容器。
//TabBar.vue
<template>
<div class="tabBar">
<slot></slot>
</div>
</template>
<script>
export default {
name:"TabBar"
}
</script>
<style scoped>
.tabBar{
display: flex;
height: 49px;
position: fixed;
left: 0;
right: 0;
bottom: 0;
text-align: center;
box-shadow: 0px -1px 1px rgba(100, 100, 100, .1);
background-color: #f6f6f6;
}
</style>
再接下來(lái)就是使用了,給每一個(gè)不同的TabBarItem的插槽寫入不同的圖片和文字信息。
//MainTabBar.vue
<template>
<div class="mainTabBar">
<tab-bar>
<tab-bar-item path="/home" activeColor="#ff8198">
<img src="~assets/img/tabbar/home.svg" alt="" slot="item-icon">
<img src="~assets/img/tabbar/home_active.svg" alt="" slot="item-icon-active">
<div slot="item-text">首頁(yè)</div>
</tab-bar-item>
<tab-bar-item path="/category" activeColor="#ff8198">
<img src="~assets/img/tabbar/category.svg" alt="" slot="item-icon">
<img src="~assets/img/tabbar/category_active.svg" alt="" slot="item-icon-active">
<div slot="item-text">分類</div>
</tab-bar-item>
<tab-bar-item path="/cart" activeColor="#ff8198">
<img src="~assets/img/tabbar/shopcart.svg" alt="" slot="item-icon">
<img src="~assets/img/tabbar/shopcart_active.svg" alt="" slot="item-icon-active">
<div slot="item-text">購(gòu)物車</div>
</tab-bar-item>
<tab-bar-item path="/profile" activeColor="#ff8198">
<img src="~assets/img/tabbar/profile.svg" alt="" slot="item-icon">
<img src="~assets/img/tabbar/profile_active.svg" alt="" slot="item-icon-active">
<div slot="item-text">我的</div>
</tab-bar-item>
</tab-bar>
</div>
</template>
<script>
import TabBar from "components/common/tabbar/TabBar"
import TabBarItem from "components/content/tabbar/TabBarItem"
export default {
name:"MainTabBar",
components:{
TabBar,
TabBarItem
}
}
</script>
<style>
</style>
導(dǎo)航欄一般都在主頁(yè)中使用,所以我們把這個(gè)導(dǎo)航欄放在App.vue
<template>
<div id="app">
<main-tab-bar></main-tab-bar>
</div>
</template>
<script>
import MainTabBar from "components/content/tabbar/MainTabBar";
export default {
name: 'App',
components:{
MainTabBar
}
}
總結(jié):這樣看來(lái),我們寫一個(gè)導(dǎo)航欄用了3個(gè)文件,這可能看起來(lái)是麻煩的,但這也大大提高了代碼的可讀性,如果我們還需要在該項(xiàng)目別的地方使用導(dǎo)航欄,我們只需要直接創(chuàng)建一個(gè)MainTabBar類似的文件,然后把你要的圖片和文字寫進(jìn)入即可,甚至于在別的項(xiàng)目用到時(shí),我們可以直接將文件拷貝過(guò)去就能夠直接使用,連CSS樣式都不需要我們?nèi)?,這就大大提高了我們的開發(fā)效率。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue2 設(shè)置router-view默認(rèn)路徑的實(shí)例
今天小編就為大家分享一篇vue2 設(shè)置router-view默認(rèn)路徑的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue-drag-chart 拖動(dòng)/縮放圖表組件的實(shí)例代碼
這篇文章主要介紹了vue-drag-chart 拖動(dòng)/縮放的圖表組件的實(shí)例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Vue自定義el-table表格表頭高度的多種實(shí)現(xiàn)方法
在Vue項(xiàng)目中,使用Element?UI的el-table組件可以輕松創(chuàng)建功能豐富的表格,然而,默認(rèn)情況下,el-table的表頭高度是固定的,本文將詳細(xì)介紹如何自定義el-table表頭的高度,提供多種實(shí)現(xiàn)方法,需要的朋友可以參考下2024-10-10
詳解項(xiàng)目升級(jí)到vue-cli3的正確姿勢(shì)
這篇文章主要介紹了詳解項(xiàng)目升級(jí)到vue-cli3的正確姿勢(shì),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
element-ui tooltip修改背景顏色和箭頭顏色的實(shí)現(xiàn)
這篇文章主要介紹了element-ui tooltip修改背景顏色和箭頭顏色的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Vue 2.0學(xué)習(xí)筆記之使用$refs訪問(wèn)Vue中的DOM
這篇文章主要介紹了Vue 2.0學(xué)習(xí)筆記之使用$refs訪問(wèn)Vue中的DOM,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
MAC+PyCharm+Flask+Vue.js搭建系統(tǒng)
最近新做了個(gè)項(xiàng)目,使用的是MAC+PyCharm+Flask+Vue.js搭建系統(tǒng),本文就來(lái)分享一下搭建步驟,感興趣的可以了解一下2021-05-05

