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>

### 实现自定义 TabBar 的方法 在 uView 框架中实现自定义 TabBar 需要遵循特定的步骤来确保组件能够正常工作并响应用户的交互。下面是一个详细的说明以及示例代码。 #### 创建 `tabBar` 组件 为了使 TabBar 更加灵活,可以先创建一个独立的 `components/tab-bar.vue` 文件,在其中编写自定义样式和功能逻辑: ```html <template> <view class="custom-tab-bar"> <block v-for="(item, index) in list" :key="index"> <view @click="switchTab(item.path)" :class="[selected === item.pagePath ? 'active' : '', 'tab-item']"> {{ item.text }} </view> </block> <!-- 中间按钮 --> <button v-if="midBtn" class="middle-btn">+</button> </view> </template> <script> export default { props: ['list', 'midBtn'], data() { return { selected: '' } }, methods: { switchTab(path) { this.selected = path; uni.switchTab({ url: '/pages/' + path, success(res){ console.log('跳转成功'); }, fail(err){ console.error('跳转失败:', err); } }); } } } </script> <style lang="scss" scoped> .custom-tab-bar{ display: flex; justify-content: space-around; align-items: center; height: 98rpx; background-color:#fff; .tab-item{ font-size:24rpx; color:#7A7E83; } .active{ color:#007AFF; } .middle-btn{ position:absolute; top:-40rpx; width:100rpx; height:100rpx; border-radius:50%; background:red; color:white; line-height:100rpx; text-align:center; } } </style> ``` 此部分展示了如何构建一个基础版的可点击切换页面的 TabBar 并设置了中间悬浮按钮[^1]。 #### Vuex 状态管理配置 为了让不同角色看到不同的菜单项,可以在 Vuex store 下建立专门用于存储 TabBar 数据的状态树。通过读取本地缓存中的用户类型决定加载哪一组 TabBar 菜单项。 ```javascript // stores/tabBar.js import tabBer from '@/util/tabBer'; // 自定义tabBar对象数组 const userType = uni.getStorageSync('user_type'); let type = userType >= 2 ? 'tchList' : 'stuList'; const state = { list: tabBer[type], midBtn: type === 'stuList' ? false : true }; export default { namespaced: true, state }; ``` 这段脚本实现了根据不同类型的用户显示相应权限下的导航栏选项。 #### 主应用入口注册全局组件与状态初始化 最后一步是在项目的主文件 `App.vue` 或者 `main.js` 中完成对上述两个模块的集成操作。这通常涉及到将新编写的 TabBar 注册成全局可用组件,并且引入 Vuex 来集中管理和同步各个视图之间的共享数据[^2]。 ```javascript // main.js import Vue from 'vue'; import App from './App'; import store from './store/index'; import uView from "uview-ui"; Vue.config.productionTip = false; Vue.use(uView); App.mpType = 'app'; const app = new Vue({ ...App, store }); app.$mount(); ``` 以上就是利用 uView 和 Uni-app 结合 Vuex 构建动态变化的底部标签页的方法概述及其具体实践案例。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值