用Vue封裝導(dǎo)航欄組件
前言:把一個(gè)功能模塊使用組件化的思想充分封裝,如導(dǎo)航欄,這無(wú)論對(duì)我們的開(kāi)發(fā)思想還是效率都有許多好處,在開(kāi)發(fā)中,我們要盡量多得運(yùn)用組件化的開(kāi)發(fā)思想,不要把所有代碼都寫(xiě)在同一個(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的插槽寫(xiě)入不同的圖片和文字信息。
//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">分類(lèi)</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)物車(chē)</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),我們寫(xiě)一個(gè)導(dǎo)航欄用了3個(gè)文件,這可能看起來(lái)是麻煩的,但這也大大提高了代碼的可讀性,如果我們還需要在該項(xiàng)目別的地方使用導(dǎo)航欄,我們只需要直接創(chuàng)建一個(gè)MainTabBar類(lèi)似的文件,然后把你要的圖片和文字寫(xiě)進(jìn)入即可,甚至于在別的項(xiàng)目用到時(shí),我們可以直接將文件拷貝過(guò)去就能夠直接使用,連CSS樣式都不需要我們?nèi)?xiě),這就大大提高了我們的開(kāi)發(fā)效率。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue單頁(yè)應(yīng)用加百度統(tǒng)計(jì)代碼(親測(cè)有效)
這篇文章主要介紹了vue單頁(yè)應(yīng)用加百度統(tǒng)計(jì)代碼的解決方法,需要的朋友參考下吧2018-01-01Vue2.x Todo之自定義指令實(shí)現(xiàn)自動(dòng)聚焦的方法
我們希望用戶雙擊 todo 進(jìn)入編輯狀態(tài)后輸入框自動(dòng)獲取焦點(diǎn),而不是需要先手動(dòng)點(diǎn)一下。這篇文章主要介紹了Vue 2.x Todo之自定義指令實(shí)現(xiàn)自動(dòng)聚焦,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2019-01-01vue動(dòng)畫(huà)—通過(guò)鉤子函數(shù)實(shí)現(xiàn)半場(chǎng)動(dòng)畫(huà)操作
這篇文章主要介紹了vue動(dòng)畫(huà)—通過(guò)鉤子函數(shù)實(shí)現(xiàn)半場(chǎng)動(dòng)畫(huà)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08關(guān)于vue項(xiàng)目中搜索節(jié)流的實(shí)現(xiàn)代碼
這篇文章主要介紹了關(guān)于vue項(xiàng)目中搜索節(jié)流的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09關(guān)于vue-property-decorator的基礎(chǔ)使用實(shí)踐
這篇文章主要介紹了關(guān)于vue-property-decorator的基礎(chǔ)使用實(shí)踐,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue緩存之keep-alive的理解和應(yīng)用詳解
這篇文章主要介紹了vue緩存之keep-alive的理解和應(yīng)用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11vue仿element實(shí)現(xiàn)分頁(yè)器效果
這篇文章主要介紹了vue仿element實(shí)現(xiàn)分頁(yè)器效果,實(shí)現(xiàn)思路是定一個(gè)值foldPage, 意為當(dāng)前最多顯示的標(biāo)簽數(shù),當(dāng)總頁(yè)數(shù)超過(guò)即顯示省略.省略分為左邊省略(folder1)和右邊省略(folder2),具體實(shí)例代碼大家參考下本文2018-09-09