tabbar.vue <template> <view class="TabBar"> <view class="tab" v-for="(item,index) in TabBarList" :key="index" @tap="navigatorTo(item.url)"> <!-- 判斷是否有點擊,若是沒有就不是激活樣式,點擊就是激活的樣式 --> <image class="imgsize" v-if="item.type == 0" :src="current == index ? item.selectIcon : item.icon" mode="widthFix"></image> <!-- 設置一個狀態值(type),判斷加號是否展現 --> <image class="addimgsize" v-if="item.type == 1" src="../../static/add.png" mode="widthFix"></image> <view class="text">{{item.name}}</view> </view> </view> </template> <script> export default { props: { current: { type: Number, default: 0 //默認第一個頁面tabbar激活 } }, data() { return { TabBarList: [{ type: 0, icon: '../../static/home.png', selectIcon: '../../static/home-active.png', name: '首頁', url: '../index/index' }, { type: 0, icon: '../../static/bagtab.png', selectIcon: '../../static/bagtab-active.png', name: '供需', url: '../demand/demand' }, { type: 1, icon: '../../static/add.png', selectIcon: '../../static/add.png', name: '發佈供需', url: '../edit/edit' }, { type: 0, icon: '../../static/company.png', selectIcon: '../../static/company-active.png', name: '企業', url: '../company/company' }, { type: 0, icon: '../../static/person.png', selectIcon: '../../static/person-active.png', name: '個人', url: '../personal/personal' }, ] } }, methods: { navigatorTo(e) { uni.redirectTo({ url: e, }); } } } </script> <style scoped> .TabBar { position: fixed; bottom: 0; height: 80upx; background-color: #fff; width: 100%; display: flex; justify-content: space-around; align-items: center; } .tab { display: flex; flex-direction: column; align-items: center; } .imgsize { width: 40upx; height: 40upx; } .addimgsize { width: 70upx; height: 70upx; margin-top: -30upx; } .text { font-size: 12px; } </style>
須要使用到tabbar的組件裏,給tabbar組件傳值,設置點擊時的激活樣式vue
<template> <view> 第二個頁面 <tabbar :current="1"></tabbar> </view> </template> <script> import tabbar from '../component/tabbar.vue' export default { data() { return { } }, components: { tabbar }, methods: { } } </script> <style> </style>