uni-app 封装tabbar组件(vue3)

1、在page页面配置tabbar 只配置路径就行

"tabBar": {
		"color": "#747174",
		"selectedColor": "#6cc2c1",
		"borderStyle": "black",
		"backgroundColor": "#F8F8F8",
        "list":[
            {"pagePath": "pages/index/index"},
            {"pagePath": "pages/news/index"},
            {"pagePath": "pages/map/index"},
            {"pagePath": "pages/activity/index"},
            {"pagePath": "pages/my/index"},
        ]
}

2、创建tabbar组件,并且添加 uni.hideTabBar()  将系统初始的tabbar隐藏

首先创建tabbarList存放我们tabbar的配置参数

 tabbarList: [
    {
      pagePath: '/pages/index/index',
      text: '主页',
      iconPath: '../static/tabbar/home.png',
      selectedIconPath: '../static/tabbar/homeSelected.png',
    },
    {
      pagePath: '/pages/news/index',
      text: '新闻',
      iconPath: '../static/tabbar/news.png',
      selectedIconPath: '../static/tabbar/newsSelected.png',
    },
    {
      pagePath: '/pages/map/index',
      text: '地图',
      iconPath: '../static/tabbar/map.png',
      selectedIconPath: '../static/tabbar/mapSelected.png',
    },
    {
      pagePath: '/pages/activity/index',
      text: '活动',
      iconPath: '../static/tabbar/activity.png',
      selectedIconPath: '../static/tabbar/activitySelected.png',
    },
    {
      pagePath: '/pages/my/index',
      text: '我的',
      iconPath: '../static/tabbar/my.png',
      selectedIconPath: '../static/tabbar/mySelected.png',
    },
  ],

然后使用uni.switchTab方法进行切换,下面是完整的tabbarList的代码

<template>
  <view class="tabbar">
    <view class="tabbar-item" v-for="(item, index) in data.tabbarList" :key="index" @click="changeTabbar(item)">
      <img class="icon" :src="index == props.currentPage ? item.selectedIconPath : item.iconPath" />
      <view class="title" :style="index == props.currentPage ? { color: '#6cc2c1' } : ''">{{ item.text }}</view>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { reactive, defineProps, onMounted } from 'vue'

const props = defineProps({
  currentPage: Number,
})
const data = reactive({
  tabbarList: [
    {
      pagePath: '/pages/index/index',
      text: '主页',
      iconPath: '../static/tabbar/home.png',
      selectedIconPath: '../static/tabbar/homeSelected.png',
    },
    {
      pagePath: '/pages/news/index',
      text: '新闻',
      iconPath: '../static/tabbar/news.png',
      selectedIconPath: '../static/tabbar/newsSelected.png',
    },
    {
      pagePath: '/pages/map/index',
      text: '地图',
      iconPath: '../static/tabbar/map.png',
      selectedIconPath: '../static/tabbar/mapSelected.png',
    },
    {
      pagePath: '/pages/activity/index',
      text: '活动',
      iconPath: '../static/tabbar/activity.png',
      selectedIconPath: '../static/tabbar/activitySelected.png',
    },
    {
      pagePath: '/pages/my/index',
      text: '我的',
      iconPath: '../static/tabbar/my.png',
      selectedIconPath: '../static/tabbar/mySelected.png',
    },
  ],
})

const changeTabbar = function (item: any) {
  uni.switchTab({
    url: item.pagePath,
  })
}

onMounted(() => {
  uni.hideTabBar();
})
</script>

<style lang="less" scoped>
.tabbar {
  position: fixed;
  left: 0;
  bottom: 0;
  display: flex;
  justify-content: space-around;
  width: 100%;
  box-sizing: border-box;
  padding: 15rpx 0rpx 55rpx 0rpx;
  background-color: #f8f8f8;
  border-top: #cfcfcf 1px solid;
  .tabbar-item {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    .icon {
      width: 28px;
      height: 28px;
    }
    .title {
      font-size: 10px;
    }
  }
}
</style>

3、接下来 在需要使用的组件中引入即可 ,但是我们在使用时要传递一个current参数,用于判断我们所选中的tabbar。这里的current参数就是这一项在tabbarList中的index索引值。

<w-tabbar :currentPage="1"></w-tabbar>

4、现在我们就可以正常使用了 但是,在我们首次加载页面的时候会出现系统默认tabbar 闪现的情况

我们直接在app中添加uni.hideTabBar()   我自己这样使用之后就不会闪了

onShow(() => {
  uni.hideTabBar();
});

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值