uniapp真机调试app动态显示隐藏tabbar某一项

本文介绍了如何在pages.json中设置tabBar中特定项的visible属性,根据用户权限动态显示或隐藏。同时,通过uni.setTabBarItem函数实现微信开发者工具中的动态效果,并指出在测试阶段在模拟器上的问题。
摘要由CSDN通过智能技术生成

1.pages.json中设置需要显示隐藏项的visible属性, 以list中index:2为例

"tabBar": {
    "color": "#9FA8BB",
    "selectedColor": "#3E71FE",
    "backgroundColor": "#FFFFFF",
    "borderStyle": "white",
    "fontSize": "10px",
    "iconWidth": "23px",
    "spacing": "6.5px",
    "height": "50px",
    "list": [
      {
        "pagePath": "view/xxx/xxx",
        "iconPath": "static/img/index/home.png",
        "selectedIconPath": "static/img/index/home-active.png",
        "text": "xxx"
      },
      {
        "pagePath": "view/xxx/xxx",
        "iconPath": "static/img/index/data.png",
        "selectedIconPath": "static/img/index/data-active.png",
        "text": "xxx"
      },
      // #ifdef APP-PLUS
      {
        "pagePath": "view/xxx/xxx",
        "iconPath": "static/img/index/yewuban-kqdk-daka.png",
        "selectedIconPath": "static/img/index/yewuban-kqdk-daka-active.png",
        "text": "xxx",
        "visible": false
      },
      // #endif
      {
        "pagePath": "view/xxx/xxx",
        "iconPath": "static/img/index/my.png",
        "selectedIconPath": "static/img/index/my-active.png",
        "text": "xxx"
      }
    ]
  },

2.uni.setTabBarItem 根据项目角色的权限,动态显示隐藏该项

if(xxxxxx){
   console.log('添加tab');
   uni.setTabBarItem({
     index: 2,
     visible: true
   });
}else{
   console.log('隐藏tab');
   uni.setTabBarItem({
     index: 2,
     visible: false
   });
}

3.测试app有效,微信开发者工具模拟器上没有效果

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uniapp 中,可以通过控制 tabBar 的 list 来实现某一项的显示和隐藏。以下是一个示例代码: 1. 在 App.vue 文件中,配置 tabBar 的 list 项,例如: ```vue <template> <div> <tab-bar :list="tabBarList" :selected="selectedTab" @click="tabBarClick"></tab-bar> <router-view></router-view> </div> </template> <script> export default { data() { return { selectedTab: 0, tabBarList: [ { iconPath: "home", selectedIconPath: "home-fill", text: "首页", pagePath: "/pages/home/home" }, { iconPath: "category", selectedIconPath: "category-fill", text: "分类", pagePath: "/pages/category/category" }, { iconPath: "cart", selectedIconPath: "cart-fill", text: "购物车", pagePath: "/pages/cart/cart" }, { iconPath: "profile", selectedIconPath: "profile-fill", text: "个人中心", pagePath: "/pages/profile/profile" } ] }; }, methods: { tabBarClick(index) { this.selectedTab = index; } } }; </script> ``` 2. 如果要隐藏一项,可以在 `tabBarList` 中移除对应的项: ```vue export default { data() { return { selectedTab: 0, tabBarList: [ // 隐藏第二个 tab { iconPath: "home", selectedIconPath: "home-fill", text: "首页", pagePath: "/pages/home/home" }, { iconPath: "cart", selectedIconPath: "cart-fill", text: "购物车", pagePath: "/pages/cart/cart" }, { iconPath: "profile", selectedIconPath: "profile-fill", text: "个人中心", pagePath: "/pages/profile/profile" } ] }; }, methods: { tabBarClick(index) { this.selectedTab = index; } } }; ``` 这样,第二个 tab 就会被隐藏起来。 通过以上方法,你可以在 Uniapp 中动态控制 tabBar 的显示和隐藏,根据需要调整 tabBar 的样式和其他配置信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值