uView自定义底部tabbar

本文介绍了在uni-app中自定义底部tabbar的详细步骤,包括将静态图标放入根目录的static文件夹,创建components文件夹内的tabbar.vue组件,隐藏原生tabbar,全局引用组件,配置pages.json,以及在页面中使用自定义tabbar的方法。通过这些步骤,可以实现个性化且功能完整的tabbar组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.根目录下的static文件夹放入自定义图标

 2.根目录下的components文件夹创建tabbar.vue(注意created要隐藏原生tabbar)

<template>
    <view>
        <u-tabbar :value="current?current:0" @change="changeTab" :fixed="true" :placeholder="false"
                  :safeAreaInsetBottom="true" :border="false">
            <u-tabbar-item v-for="(item,index) in list" :key="index" :text="item.text">
                <image class="u-page__item__slot-icon" slot="active-icon" :src="item.iconPath"></image>
                <image class="u-page__item__slot-icon" slot="inactive-icon" :src="item.selectedIconPath"></image>
            </u-tabbar-item>
        </u-tabbar>
    </view>
</template>

<script>
    export default {
        name: "tabbar",
        props: {
            current: Number
        },
        created() {
            //隐藏原生tabbar
            uni.hideTabBar()
        },
        data() {
            return {
                list: [{
                    selectedIconPath: "../../static/images/tabbar/install.png",
                    iconPath: "../../static/images/tabbar/install-active.png",
                    text: '安装单',
                    customIcon: false,
                    pagePath: "pages/install/index"
                },
                    {
                        selectedIconPath: "../../static/images/tabbar/repair.png",
                        iconPath: "../../static/images/tabbar/repair-active.png",
                        text: '维修单',
                        customIcon: false,
                        pagePath: "pages/repair/index"
                    },
                    {
                        selectedIconPath: "../../static/images/tabbar/my.png",
                        iconPath: "../../static/images/tabbar/my-active.png",
                        text: '我的',
                        customIcon: false,
                        pagePath: "pages/my/index"
                    }
                ],
            }
        },
        methods: {
            changeTab(e) {
                console.log(e)
                uni.switchTab({
                    url: '/' + this.list[e].pagePath,
                })
            }
        }
    }
</script>

<style scoped>
    .u-page__item__slot-icon{
        width: 20px!important;
        height: 20px!important;
    }
</style>

3.main.js全局引用自定义tabbar组件

import tabbar from './components/tabbar/tabbar'
Vue.use('tab-bar',tabbar)

 4.配置pages.json

 "tabBar": {
    "custom": true,
    "list": [
      {
        "pagePath": "pages/install/index"
      },
      {
        "pagePath": "pages/repair/index"
      },
      {
        "pagePath": "pages/my/index"
      }
    ]
  }

 5.在页面中使用

<template>
    <view>
        <tabbar :current="0"></tabbar>
    </view>
</template>

<script>
    export default {
        name: "index"
    }
</script>

<style scoped>

</style>

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值