DCloud_UNI_JBB
DCloud_UNI_JBB
  • 发布:2026-03-26 15:50
  • 更新:2026-04-07 14:55
  • 阅读:338

实现微信小程序自定义 tabBar

分类:uni-app

前言

本文分享如何在uniapp vue3 实现自定义微信小程序 tabBar。

配置信息

pages.json 中添加 tabBar 的相关配置,例如

{  
  "pages": [  
    {  
      "path": "pages/index/index",  
      "style": {  
        "navigationBarTitleText": "首页"  
      }  
    },  
    {  
      "path": "pages/mine/index",  
      "style": {  
        "navigationBarTitleText": "我的"  
      }  
    }  
  ],  
  "tabBar": {  
    "custom": true,  
    "color": "#7A7E83",  
    "selectedColor": "#3cc51f",  
    "borderStyle": "black",  
    "backgroundColor": "#ffffff",  
    "list": [  
      {  
        "pagePath": "pages/index/index",  
        "iconPath": "static/icon_component.png",  
        "selectedIconPath": "static/icon_component_HL.png",  
        "text": "首页"  
      },  
      {  
        "pagePath": "pages/mine/index",  
        "iconPath": "static/icon_API.png",  
        "selectedIconPath": "static/icon_API_HL.png",  
        "text": "我的"  
      }  
    ]  
  }  
}

添加 tabBar 代码文件

在根目录添加 custom-tab-bar 文件夹,下面包含微信小程序原生文件

编写 tabBar 代码

这一步需要获取自定义 tabBar 组件实例,通过示例来更新选中的 tab,微信小程序可以通过 this 操作,uniapp 也支持直接操作微信小程序组件示例,如下代码

<template>  
  <view>  
    <text>首页</text>  
  </view>  
</template>  

<script setup>  
import { getCurrentInstance } from "vue";  
import { onShow } from "@dcloudio/uni-app";  

const instance = getCurrentInstance();  

onShow(() => {  
  const tabBar = instance?.proxy?.$scope?.getTabBar?.();  // 获取组件示例函数返回值  
  if (tabBar) {  
    tabBar.setData({  
      selected: 0,  
    });  
  }  
});  
</script>

其他 tab 页同理

示例项目

参考附件

0 关注 分享

要回复文章请先登录注册

hello_wzq

hello_wzq

回复 hello_wzq :
已解决
2026-04-07 14:55
DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 hello_wzq :
你是哪个端?h5吗?
2026-04-07 11:03
hello_wzq

hello_wzq

node_modules/@dcloudio/uni-h5/dist/uni-h5.es.js (1:79): "openBlock" is not exported by "node_modules/@dcloudio/uni-mp-vue/dist/vue.runtime.esm.js", imported by "node_modules/@dcloudio/uni-h5/dist/uni-h5.es.js".
14:46:54.584 at E:/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5/dist/uni-h5.es.js:1:79
14:46:54.584 1: import { withModifiers, createVNode, getCurrentInstance, ref, defineComponent, openBlock, createElementBlock, provide...
14:46:54.585 ^
14:46:54.585 2: import { isArray, isString, extend, remove, stringifyStyle, parseStringStyle, isPlainObject, isFunction, capitalize, ...
14:46:54.585 3: import { once, UNI_STORAGE_LOCALE, I18N_JSON_DELIMITERS, Emitter, passive, resolveComponentInstance, normalizeStyles,...
2026-04-03 14:49