先在根目录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 //选择第几个导航
})
})
}