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

主要思路:把紅色的部分當成一個個組件,而他們只是圖片和文字不同,所以我們可以把他們封裝成同一個組件,然后向組件里傳入圖片信息和文字信息即可(可以用插槽)。
//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>
接下來就是封裝一個把這4個選項放在同一個地方的容器。
//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>
再接下來就是使用了,給每一個不同的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">首頁</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">購物車</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>
導航欄一般都在主頁中使用,所以我們把這個導航欄放在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
}
}
總結:這樣看來,我們寫一個導航欄用了3個文件,這可能看起來是麻煩的,但這也大大提高了代碼的可讀性,如果我們還需要在該項目別的地方使用導航欄,我們只需要直接創(chuàng)建一個MainTabBar類似的文件,然后把你要的圖片和文字寫進入即可,甚至于在別的項目用到時,我們可以直接將文件拷貝過去就能夠直接使用,連CSS樣式都不需要我們?nèi)?,這就大大提高了我們的開發(fā)效率。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Vue自定義el-table表格表頭高度的多種實現(xiàn)方法
在Vue項目中,使用Element?UI的el-table組件可以輕松創(chuàng)建功能豐富的表格,然而,默認情況下,el-table的表頭高度是固定的,本文將詳細介紹如何自定義el-table表頭的高度,提供多種實現(xiàn)方法,需要的朋友可以參考下2024-10-10
element-ui tooltip修改背景顏色和箭頭顏色的實現(xiàn)
這篇文章主要介紹了element-ui tooltip修改背景顏色和箭頭顏色的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12
MAC+PyCharm+Flask+Vue.js搭建系統(tǒng)
最近新做了個項目,使用的是MAC+PyCharm+Flask+Vue.js搭建系統(tǒng),本文就來分享一下搭建步驟,感興趣的可以了解一下2021-05-05

