微信小程序/uniapp动态修改tabBar信息及常见报错

目录

uni.setTabBarItem(options)

介绍:

wx.setTabBarItem是不支持修改url路径的,所以使用uniapp的API。

动态设置 tabBar 某一项的内容:uniapp官网

setTabBarItem 兼容性:

Web Android iOS
4.0 3.91 4.11

参数:

options里的具体参数:

名称 类型 必备 默认值 兼容性 描述
index number - - tabBar 的哪一项,从左边算起,索引从0开始
text string - - tab 上按钮文字
iconPath string - - 图片路径
selectedIconPath string - - 选中时的图片路径
pagePath string - - 页面绝对路径
iconfont SetTabBarItemIconFontOptions - - 字体图标,优先级高于 iconPath
visible boolean - - tab 是否显示
success (result: AsyncApiSuccessResult) => void - - 接口调用成功的回调函数
fail (result: SetTabBarFail) => void - - 接口调用失败的回调函数
complete (result: AsyncApiResult) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)

iconfont 包含参数:

名称 类型 必备 默认值 兼容性 描述
text string - - 字库 Unicode 码
selectedText string - - 选中后字库 Unicode 码
fontSize string - - 字体图标字号(px)
color string - - 字体图标颜色
selectedColor string - - 字体图标选中颜色

示例:

javascript 复制代码
uni.setTabBarItem({
  index: 0,
  text: 'text',
  iconPath: '/path/to/iconPath',
  selectedIconPath: '/path/to/selectedIconPath',
  pagePath: '/path/newPagePath',
  success: ()=>{
    console.log('success');
  },
  fail: ()=>{
    console.log('success');
  },
  complete: ()=>{
    console.log('success');
  },
})

报错

使用时遇到了报错:"setTabBarItem:fail not TabBar page"

原因:setTabBarItem方法 只能在tabbar页面调用(有看到有人说在app中可以在非tabbar页面调用,但是我只是在微信小程序中使用,所以不清楚能不能用)

解决方法:在tabBar页面调用(没办法,微信小程序不支持)

相关推荐
百锦再7 小时前
抖音小程序开发全景透视:生态解析、技术架构与商业实践
人工智能·ai·微信小程序·小程序·架构·模型·抖音
计算机毕设指导614 小时前
基于微信小程序的家政服务与互助平台【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
wetyuo16 小时前
【随手记】uniapp + V3 使用TailwindCss3
uni-app·vue·css3·vite
计算机毕设指导617 小时前
基于微信小程序的个性化漫画阅读推荐系统【源码文末联系】
java·python·微信小程序·小程序·tomcat·maven·intellij-idea
Lucky小黄人1 天前
微信小程序开发常见报错
微信小程序·小程序
Stanford_11062 天前
【2026新年启程】学习之路,探索之路,技术之路,成长之路……都与你同行!!!
前端·c++·学习·微信小程序·排序算法·微信开放平台
初学者52132 天前
微信小程序调用录音没反应,本地开发测试却没问题,operateRecorderfail appid privacy api banned
微信小程序·小程序
小恒恒2 天前
2025 Vibe Coding 有感
前端·uni-app·trae
一颗小青松2 天前
uniapp使用uni-im
uni-app
2501_916007473 天前
iPhone APP 性能测试怎么做,除了Instruments还有什么工具?
android·ios·小程序·https·uni-app·iphone·webview