小程序自定义底部导航根据权限动态生成

先在根目录pages同级创建文件夹custom-tab-bar
在这里插入图片描述

<view class="tab-bar">
  <view class="tab-bar-border"></view>
  <view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
    <image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></image>
    <view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</view>
  </view>
</view>
.tab-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 80px;
  background: white;
  display: flex;
  padding-top: 10rpx;
  box-sizing: border-box;
  flex-direction: row;
  padding-bottom: env(safe-area-inset-bottom);
  pointer-events: auto;
}

.tab-bar-border {
  background-color: rgba(0, 0, 0, 0.33);
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 1px;
  transform: scaleY(0.5);
}

.tab-bar-item {
  flex: 1;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.tab-bar-item image {
  width: 27px;
  height: 27px;
}

.tab-bar-item view {
  font-size: 10px;
}


Component({
    lifetimes: {
        attached(){
            this.getUser("", "") //未上传 获取当前登录用户的底部导航权限动态切换data里的list为mdlist或者userlist
        }
    }, 
    data: {
        selected: 0,
        color: "#7A7E83",
        selectedColor: "#07C160",
        list: [],
        num: 0,
        mdlist: [{
            pagePath: "/pages/mdsy/mdsy",
            iconPath: "/images/tabbar/icon1.png",
            selectedIconPath: "/images/tabbar/icon2.png",
            text: "首页"
        }, {
            pagePath: "/pages/main/main",
            iconPath: "/images/tabbar/icon9.png",
            selectedIconPath: "/images/tabbar/icon10.png",
            text: "商城"
        }, {
            pagePath: "/pages/ddcenter/ddcenter",
            iconPath: "/images/tabbar/icon3.png",
            selectedIconPath: "/images/tabbar/icon4.png",
            text: "购物车"
        }, {
            pagePath: "/pages/mycenter/mycenter",
            iconPath: "/images/tabbar/icon5.png",
            selectedIconPath: "/images/tabbar/icon6.png",
            text: "个人中心"
        }, {
            pagePath: "/pages/myshop/myshop",
            iconPath: "/images/tabbar/icon7.png",
            selectedIconPath: "/images/tabbar/icon8.png",
            text: "我的店铺"
        }],
        userlist: [{
            pagePath: "/pages/mdsy/mdsy",
            iconPath: "/images/tabbar/icon1.png",
            selectedIconPath: "/images/tabbar/icon2.png",
            text: "首页"
        }, {
            pagePath: "/pages/main/main",
            iconPath: "/images/tabbar/icon1.png",
            selectedIconPath: "/images/tabbar/icon2.png",
            text: "商城"
        }, {
            pagePath: "/pages/ddcenter/ddcenter",
            iconPath: "/images/tabbar/icon3.png",
            selectedIconPath: "/images/tabbar/icon4.png",
            text: "购物车"
        }, {
            pagePath: "/pages/mycenter/mycenter",
            iconPath: "/images/tabbar/icon5.png",
            selectedIconPath: "/images/tabbar/icon6.png",
            text: "个人中心"
        }]
    },
 

    methods: {
        switchTab(e) {
            const data = e.currentTarget.dataset
            const url = data.path
            console.log(data,'eee')
            wx.switchTab({
                url
            })
            this.setData({
                selected: data.index
            })
        },
    }
})

在底部导航onshow添加

    if (typeof this.getTabBar === 'function') {
            this.getTabBar((tabBar) => {
                tabBar.setData({
                    selected: 0  //选择第几个导航
                })
            })
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值