vue.js移動(dòng)端tab組件的封裝實(shí)踐實(shí)例
這是vue.js系列文章第二遍,第一篇講述的是如何搭建vue.js的開(kāi)發(fā)環(huán)境,計(jì)劃按進(jìn)度做成一款完整的app,當(dāng)然前提是時(shí)間允許的話。本文用到了stylus語(yǔ)法,至于為什么使用stylus而不去用sass,主要是因?yàn)閟tylus來(lái)自于Node.js社區(qū)??傊畇tylus是一款高效的CSS預(yù)處理器,具體使用不在本文討論范圍。好了,廢話不說(shuō)了,下面講述怎么封裝tababr的切換。
底部tab進(jìn)行頁(yè)面切換,會(huì)用到vue里面的路由,也就是vue-router
我們?cè)诎惭bvue-cli時(shí)選中默認(rèn)安裝vue-router即可。
安裝完畢后,打開(kāi)我的項(xiàng)目,我們需要在router目錄的index.vue中配置路由信息,具體配置信息如下

從上面圖片,我們可以看到,我們一共配置了4子頁(yè)面,其中redirect為配置默認(rèn)組件的路由。
路由配置完成后,我們需要封裝tab組件了
因?yàn)閠ab組件屬于基礎(chǔ)組件,所以我們新建了文件夾tab,然后在tab文件夾下面新建了tabbar組件和tababritem組件。我們先說(shuō)tababritem組件的封裝
tabbaritem封裝
我們知道tababritem有一張正常顯示圖片,選中后的圖片,和圖片下的文字,其中屬性id用來(lái)記錄當(dāng)前tabbaritem的組件名,屬性isRouter用來(lái)記錄當(dāng)前選中是否是這個(gè)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>
接下來(lái),我們要封裝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)聽(tīng)子類tabbaritem的點(diǎn)擊方法,來(lái)控制當(dāng)前哪個(gè)item的選中顏色文字的改變
app.vue代碼
<template>
<div id="app">
<router-view></router-view>
<m-tabbar @tabbarActionEvent='changeSelectedValue'>
<m-tabbar-item id='Home' :isRouter="isHome">


首頁(yè)
</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圖片,大家可以自己替換掉,下一篇,會(huì)提到導(dǎo)航部分的封裝
最終運(yùn)行效果如下

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue.js默認(rèn)路由不加載linkActiveClass問(wèn)題的解決方法
這篇文章主要給大家介紹了關(guān)于vue.js默認(rèn)路由不加載linkActiveClass問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-12-12
Ant Design Vue日期組件的時(shí)間限制方式
這篇文章主要介紹了Ant Design Vue日期組件的時(shí)間限制方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Vue使用axios post方式將表單中的數(shù)據(jù)以json格式提交給后端接收操作實(shí)例
這篇文章主要介紹了Vue使用axios post方式將表單中的數(shù)據(jù)以json格式提交給后端接收操作,結(jié)合實(shí)例形式分析了vue基于axios庫(kù)post傳送表單json格式數(shù)據(jù)相關(guān)操作實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2023-06-06
vue全局方法plugins/utils的實(shí)現(xiàn)示例
很多時(shí)候我們會(huì)在全局調(diào)用一些方法,本文主要介紹了vue全局方法plugins/utils的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
Vue項(xiàng)目接入Paypal實(shí)現(xiàn)示例詳解
這篇文章主要介紹了Vue項(xiàng)目接入Paypal實(shí)現(xiàn)示例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
詳解Vue的Pinia如何做到刷新不丟數(shù)據(jù)
Pinia 是 Vue 3 的官方推薦狀態(tài)管理庫(kù),旨在替代 Vuex,提供更簡(jiǎn)單、直觀的狀態(tài)管理解決方案,Pinia 的設(shè)計(jì)理念是簡(jiǎn)單、易于學(xué)習(xí)和使用,本文給大家詳細(xì)介紹了Vue的Pinia如何做到刷新不丟數(shù)據(jù),需要的朋友可以參考下2025-01-01

