微信小程序/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页面调用(没办法,微信小程序不支持)

相关推荐
白杨木影子被拉长2 小时前
多状态映射不同样式(scss语法)
vue.js·uni-app
流口水的兔子2 小时前
作为一个新手,如果让你去用【微信小程序通过BLE实现与设备通讯】,你会怎么做,
前端·物联网·微信小程序
一念杂记2 小时前
免费开源!微信小程序商城源码,快速搭建你的线上商城系统!
微信小程序·uni-app
The_era_achievs_hero6 小时前
微信小程序61~70
微信小程序·小程序
编程猪猪侠7 小时前
Taro+Vue3实现微信小程序富文本编辑器组件开发指南
vue.js·微信小程序·taro
aklry8 小时前
uniapp三步完成生成一维码图片
uni-app
汤姆yu17 小时前
基于微信小程序的学校招生系统
微信小程序·小程序·招生小程序
雪芽蓝域zzs18 小时前
uniapp 国密sm2加密
uni-app
一渊之隔1 天前
微信小程序在用户拒绝授权后无法使用wx.opensetting再次获取定位授权
微信小程序·小程序
打不着的大喇叭1 天前
uniapp的光标跟随和打字机效果
前端·javascript·uni-app