查看: 283|回复: 0

[uniapp] uniapp动态设置tabbar

[复制链接]

748

主题

39

回帖

3907

积分

管理员

总舵主

积分
3907
QQ
发表于 2026-1-5 17:22:00 | 显示全部楼层 |阅读模式
首页代码:

  1. // 动态设置tabBar第一项
  2.                         // #ifdef APP || WEB
  3.                         let tabIndex = getApp().globalData.tabIndex;
  4.                         console.log(tabIndex)
  5.                         if (tabIndex != 0) {
  6.                                 // #ifdef APP || WEB
  7.                                 let tabBar = {
  8.                                         index: 0,
  9.                                         selectedIconPath: "/static/tabbar/default.png",
  10.                                         text: ''
  11.                                 }
  12.                                 // #endif
  13.                                 // #ifdef MP
  14.                                 let tabBar = {
  15.                                         index: 0,
  16.                                         text: '首页',
  17.                                         iconPath: "/static/tabbar/home.png",
  18.                                         selectedIconPath: "/static/tabbar/home-selected.png",
  19.                                 }
  20.                                 // #endif
  21.                                 uni.setTabBarItem(tabBar)
  22.                                 getApp().globalData.tabIndex = 0;
  23.                         }
  24.                         // #endif
复制代码
分类页代码:
  1. onLoad() {
  2.                         // 动态设置tabBar第一项
  3.                         // uni.setTabBarItem({
  4.                         //         index: 0,
  5.                         //         text: '首页',
  6.                         //         iconPath: "/static/tabbar/home.png",
  7.                         //         selectedIconPath: "/static/tabbar/home-selected.png",
  8.                         // })
  9.                 },
  10.                 onShow() {
  11.                         // 动态设置tabBar第一项
  12.                         // #ifdef APP || WEB
  13.                         let tabIndex = getApp().globalData.tabIndex;
  14.                         console.log(tabIndex)
  15.                         if (tabIndex == 0) {
  16.                                 let tabBar = {
  17.                                         index: 0,
  18.                                         text: '首页',
  19.                                         iconPath: "/static/tabbar/home.png"
  20.                                 }
  21.                                 uni.setTabBarItem(tabBar)
  22.                         }
  23.                         getApp().globalData.tabIndex = 1;
  24.                         // #endif
  25.                        
  26.                 },
复制代码
订单页代码:
  1. onShow() {
  2.                         // 动态设置tabBar第一项
  3.                         // #ifdef APP || WEB
  4.                         let tabIndex = getApp().globalData.tabIndex;
  5.                         console.log(tabIndex)
  6.                         if (tabIndex == 0) {
  7.                                 let tabBar = {
  8.                                         index: 0,
  9.                                         text: '首页',
  10.                                         iconPath: "/static/tabbar/home.png"
  11.                                 }
  12.                                 uni.setTabBarItem(tabBar)
  13.                         }
  14.                         getApp().globalData.tabIndex = 2;
  15.                         // #endif
  16.                        
  17.                 },
复制代码
我的页面代码:
  1. onShow() {
  2.                         // 动态设置tabBar第一项
  3.                         // #ifdef APP || WEB
  4.                         let tabIndex = getApp().globalData.tabIndex;
  5.                         console.log(tabIndex)
  6.                         if (tabIndex == 0) {
  7.                                 let tabBar = {
  8.                                         index: 0,
  9.                                         text: '首页',
  10.                                         iconPath: "/static/tabbar/home.png"
  11.                                 }
  12.                                 uni.setTabBarItem(tabBar)
  13.                         }
  14.                         getApp().globalData.tabIndex = 3;
  15.                         // #endif
  16.                        
  17.                 },
复制代码


道不同,不相为谋!理想要有,万一实现了呢?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:2513533699@qq.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|鲁ICP备19000917号|鲁公网安备37078302000320号

在本版发帖
关注公众号
QQ客服返回顶部