vue自定義底部導(dǎo)航欄Tabbar的實現(xiàn)代碼

如圖所示,要完成類似的一個底部導(dǎo)航切換。
首先。我們需要分為5個大的VUE文件??梢愿鶕?jù)自己的習(xí)慣來放在不同的位置。

我將5個主要的vue文件放在了5個不同的文件夾
然后,在components文件夾里新建Tabbar.vue/以及Item.vue文件
Item.vue文件如下
<template>
<div class="itemWarp flex_mid" @click='changePage'>
<span v-show='!bol'>
<slot name='normalImg'></slot>
</span>
<span v-show='bol'>
<slot name='activeImg'></slot>
</span>
<span v-text='txt'></span>
</div>
</template>
<script type="text/javascript">
export default{
name: 'Item',
props:{
txt:{
type:String
},
page:{
type:String
},
sel:{
type:String
}
},
computed:{
bol: function(){
if(this.sel == this.page){
return true;
}
return false;
}
},
methods:{
changePage:function(){
//點(diǎn)擊跳轉(zhuǎn)對應(yīng)的頁面
this.$router.push('/'+this.page);
this.$emit('change',this.page)
}
}
}
</script>
<style type="text/css">
.itemWarp{
flex-grow: 1;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.itemWarp span{
font-size: 12px;
}
</style>
Tabbar.vue文件如下
<template>
<div class="tabberWarp" >
<div class="warp">
<Item :txt='item.txt' :page='item.page' @change='getVal' v-for='item in tabbarDes':sel='selected'>
<img :src="item.normalImg" slot='normalImg'>
<img :src="item.activeImg" slot='activeImg'>
</Item>
</div>
</div>
</template>
<script type="text/javascript">
import Item from './Item.vue'
export default{
components:{
Item
},
data:function(){
return{
selected:'skin',
tabbarDes:[
{
txt:'表情',
page:'skin',
normalImg:require('../assets/images/zz_07.jpg'),
activeImg:require('../assets/images/22_03.jpg')
},
{
txt:'皮膚',
page:'phiz',
normalImg:require('../assets/images/download_skin_ic.png'),
activeImg:require('../assets/images/112_26.jpg')
},
{
txt:'詞庫',
page:'thesaurus',
normalImg:require('../assets/images/zz_09.jpg'),
activeImg:require('../assets/images/icon2_03.jpg')
},
{
txt:'賬號',
page:'account',
normalImg:require('../assets/images/zz_11.jpg'),
activeImg:require('../assets/images/cion_03.jpg')
},
{
txt:'設(shè)置',
page:'setup',
normalImg:require('../assets/images/zz_13.jpg'),
activeImg:require('../assets/images/22_03.jpg')
}
]
}
},
methods:{
getVal:function(res){
this.selected = res;
}
}
}
</script>
<style type="text/css">
.warp{
width: 100%;
border-top: 1px solid #eee;
background: #fff;
display: flex;
align-items: center;
justify-content: space-around;
font-size: 0;
}
.warp img{
width: 20px;
height: 20px;
}
.tabberWarp img{
margin-top: 10px;
margin-bottom: 5px;
}
.tabberWarp{
position: fixed;
bottom: 0;
left: 0;
width: 100%;
padding-bottom: 5px;
background: #fff;
}
</style>
Tabbar.vue文件和Item.vue的關(guān)系為父子關(guān)系.
Tabbar.vue組件 通過v-for循環(huán)tabbarDes里面的數(shù)據(jù).再通過 props 向下傳遞數(shù)據(jù)給子組件.Item.vue能接受到父組件傳遞的數(shù)據(jù).
Item.vue組件綁定點(diǎn)擊事件.
this.$router.push('/'+this.page);為跳轉(zhuǎn)到對應(yīng)的page
this.$emit('change',this.page)為使用$emit 觸發(fā)父組件的自定義事件 change,將this.page作為參數(shù)傳遞到父組件中.父組件點(diǎn)擊獲取到傳遞過來的參數(shù).再通過props傳遞給item.vue.在computed計算屬性中.返回不同的布爾值.來做底部圖片的顯示隱藏.
最后僅需要在App.vue中引入Tabbar組件即可.
<template>
<div id="app">
<router-view></router-view>
<Tabbar></Tabbar>
<div class="empty"></div>
</div>
</template>
<script>
import Tabbar from'./components/tabbar'
export default {
name: 'app',
created:function(){
this.$router.push('/')
},
components:{
Tabbar
}
}
</script>
總結(jié)
以上所述是小編給大家介紹的vue自定義底部導(dǎo)航欄Tabbar的實現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue項目拍照或上傳圖片并實現(xiàn)轉(zhuǎn)化為base64格式
這篇文章主要介紹了vue項目拍照或上傳圖片并實現(xiàn)轉(zhuǎn)化為base64格式方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
vue 解決form表單提交但不跳轉(zhuǎn)頁面的問題
今天小編就為大家分享一篇vue 解決form表單提交但不跳轉(zhuǎn)頁面的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Vue中引入bootstrap.min.css的正確姿勢分享
這篇文章主要介紹了Vue中引入bootstrap.min.css的正確姿勢,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
vue用addRoutes實現(xiàn)動態(tài)路由的示例
本篇文章主要介紹了vue用addRoutes實現(xiàn)動態(tài)路由的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
vue如何設(shè)置描點(diǎn)跳轉(zhuǎn)到對應(yīng)頁面
這篇文章主要介紹了vue如何設(shè)置描點(diǎn)跳轉(zhuǎn)到對應(yīng)頁面問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
IDEA創(chuàng)建Vue項目的兩種方式總結(jié)
這篇文章主要介紹了IDEA創(chuàng)建Vue項目的兩種方式總結(jié),具有很好的參考價值,希望對大家有所幫助。2023-04-04

