vue.js移動端tab組件的封裝實踐實例
這是vue.js系列文章第二遍,第一篇講述的是如何搭建vue.js的開發(fā)環(huán)境,計劃按進度做成一款完整的app,當然前提是時間允許的話。本文用到了stylus語法,至于為什么使用stylus而不去用sass,主要是因為stylus來自于Node.js社區(qū)??傊畇tylus是一款高效的CSS預(yù)處理器,具體使用不在本文討論范圍。好了,廢話不說了,下面講述怎么封裝tababr的切換。
底部tab進行頁面切換,會用到vue里面的路由,也就是vue-router
我們在安裝vue-cli時選中默認安裝vue-router即可。
安裝完畢后,打開我的項目,我們需要在router目錄的index.vue中配置路由信息,具體配置信息如下
從上面圖片,我們可以看到,我們一共配置了4子頁面,其中redirect為配置默認組件的路由。
路由配置完成后,我們需要封裝tab組件了
因為tab組件屬于基礎(chǔ)組件,所以我們新建了文件夾tab,然后在tab文件夾下面新建了tabbar組件和tababritem組件。我們先說tababritem組件的封裝
tabbaritem封裝
我們知道tababritem有一張正常顯示圖片,選中后的圖片,和圖片下的文字,其中屬性id用來記錄當前tabbaritem的組件名,屬性isRouter用來記錄當前選中是否是這個tababritem。
<template> <a class="m-tabbar-item" :class="{'is-active':isActive}" @click="goToRouter"> <div class="m-tabbar-item-icon" v-show="!isActive"><slot name="icon-normal"></slot></div> <div class="m-tabbar-item-icon" v-show="isActive"><slot name="icon-active"></slot></div> <div class="m-tabbar-item-text"><slot></slot></div> </a> </template> <script type="text/ecmascript-6"> export default{ props: { id: { type: String }, isRouter: { type: Boolean, default: false } }, computed: { isActive () { return this.isRouter } }, methods: { goToRouter () { this.$parent.$emit('tabbarActionEvent', this.id) // 判斷是否為路由跳轉(zhuǎn) this.$router.push(this.id) } } } </script> <style scoped lang="stylus" rel="stylesheet/stylus"> .m-tabbar-item flex: 1 text-align: center .m-tabbar-item-icon padding-top: 5px padding-bottom 1px img width: 24px height: 24px .m-tabbar-item-text font-size: 8px color:#949494 &.is-active .m-tabbar-item-text color: #fa3e25 </style>
接下來,我們要封裝tababr,tabbar里面需要包含tabbaritem,主要設(shè)置了下tabbar的樣式,具體代碼如下
tabbar的封裝
<template> <div class="m-tabbar"> <slot></slot> </div> </template> <script type="text/ecmascript-6"> export default {} </script> <style scoped lang="stylus" rel="stylesheet/stylus"> .m-tabbar display: flex flex-direction: row position: fixed bottom: 0 left: 0 right: 0 width: 100% overflow: hidden height: 50px background: #fff border-top: 1px solid #e4e4e4 </style>
最后在我們的app.vue里面引用tabbar組件,監(jiān)聽子類tabbaritem的點擊方法,來控制當前哪個item的選中顏色文字的改變
app.vue代碼
<template> <div id="app"> <router-view></router-view> <m-tabbar @tabbarActionEvent='changeSelectedValue'> <m-tabbar-item id='Home' :isRouter="isHome">   首頁 </m-tabbar-item> <m-tabbar-item id='Position' :isRouter="isPosition">   職位 </m-tabbar-item> <m-tabbar-item id='Message' :isRouter="isMessage">   消息 </m-tabbar-item> <m-tabbar-item id='Me' :isRouter="isMe">   我 </m-tabbar-item> </m-tabbar> </div> </template> <script> import mTabbar from 'common/tab/tab.vue' import mTabbarItem from 'common/tab/tabbar-item' export default { name: 'app', components: { mTabbar, mTabbarItem }, data () { return { isHome: true, isPosition: false, isMessage: false, isMe: false } }, methods: { changeSelectedValue: function (elValue) { if (elValue === 'Home') { this.isHome = true } else { this.isHome = false } if (elValue === 'Position') { this.isPosition = true } else { this.isPosition = false } if (elValue === 'Message') { this.isMessage = true } else { this.isMessage = false } if (elValue === 'Me') { this.isMe = true } else { this.isMe = false } } } } </script>
自此tababr已經(jīng)封裝完畢了,其中用到的tabbaritem圖片,大家可以自己替換掉,下一篇,會提到導(dǎo)航部分的封裝
最終運行效果如下
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue緩存的keepalive頁面刷新數(shù)據(jù)的方法
這篇文章主要介紹了vue緩存的keepalive頁面刷新數(shù)據(jù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04使用yarn?build?打包vue項目時靜態(tài)文件或圖片未打包成功的問題及解決方法
這篇文章主要介紹了使用yarn?build?打包vue項目時靜態(tài)文件或圖片未打包成功的問題及解決方法,解決方法不復(fù)雜通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-08-08VueX瀏覽器刷新如何實現(xiàn)保存數(shù)據(jù)
這篇文章主要介紹了VueX瀏覽器刷新如何實現(xiàn)保存數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07使用Vue父子組件通信實現(xiàn)todolist的功能示例代碼
這篇文章主要給大家介紹了關(guān)于如何使用Vue父子組件通信實現(xiàn)todolist的功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習或者使用Vue具有一定的參考學(xué)習價值,需要的朋友們下面來一起學(xué)習學(xué)習吧2019-04-04Vue.js 實現(xiàn)地址管理頁面思路詳解(地址添加、編輯、刪除和設(shè)置默認地址)
這篇文章主要介紹了Vue.js 實現(xiàn)地址管理頁面的思路(地址添加、編輯、刪除和設(shè)置默認地址),本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12Vue數(shù)據(jù)驅(qū)動模擬實現(xiàn)4
這篇文章主要介紹了Vue數(shù)據(jù)驅(qū)動模擬實現(xiàn)的相關(guān)資料,介紹了Array的變異方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01