**【1】根目录下新建custom-tab-bar目录 新建index(Component组件)**必须是index命名
【2】配置app.json
"tabBar": {
"custom": true, //开启自定义tabbar
"color": "#7A7E83",
"selectedColor": "#f00",
"borderStyle": "black",
"backgroundColor": "#ffffff",
"list": [
{
"pagePath": "pages/index/index",
"text": "首页",
"iconPath": "/images/tabbar/index.png",
"selectedIconPath": "/images/tabbar/index1.png"
},
{
"pagePath": "pages/user/user",
"text": "会员",
"iconPath": "/images/tabbar/user.png",
"selectedIconPath": "/images/tabbar/user1.png"
}
]
}
【3】custom-tab-bar/index.js文件代码
// custom-tab-bar/tabbar.js
Component({
/**
* 组件的属性列表
*/
properties: {
},
/**
* 组件的初始数据
*/
data: {
selected: 0,
color: "#f60",
selectedColor: "#3cc51f",
list: [
{
pagePath: "/pages/index/index",
text: "首页",
iconPath: "/images/tabbar/index.png",
selectedIconPath: "/images/tabbar/index1.png"
},
{
pagePath: "/pages/user/user",
text: "分类",
iconPath: "/images/tabbar/nav.png",
selectedIconPath: "/images/tabbar/nav1.png"
}, {
pagePath: "/pages/index/index",
text: "购物车",
iconPath: "/images/tabbar/car.png",
selectedIconPath: "/images/tabbar/car1.png"
},
{
pagePath: "/pages/user/user",
text: "会员中心",
iconPath: "/images/tabbar/user.png",
selectedIconPath: "/images/tabbar/user1.png"
}
]
},
/**
* 组件的方法列表
*/
methods: {
switchTab(e) {
const data = e.currentTarget.dataset
const url = data.path
wx.switchTab({ url })
this.setData({
selected: data.index
})
}
}
})
说明
data:主要的配置都在这里
selected 默认选中状态,0为第一个
list
这里就是你的tabBar,需要几个就在这里面添加,最少2个,最多5个
pagePath:页面的路径
[提醒注意]—>路径的前面需要添加" / “,而app.json中list的pagePath是不能以” / "开头的,一定要注意,不然wx.switchTab在调用时会出错。
iconPath:图标路径
selectedIconPath:被选中时的图标路径
text:名称
methods方法:tabBar跳转和页面跳转
e.currentTarget.dataset:通过e调取当前页数据,点击的tabbar,即要跳转的页面
data.path:获取路径
wx.switchTab():跳转到tabBar页面,并关闭其他所有非tabBar页面
url:需要跳转的tabBar页面的路径
success:成功回调函数
fail:失败回调函数
complete:接口调用结束回调函数
this.setData():把下标赋值给selected
【4】custom-tab-bar/index.wxml文件代码
<!--custom-tab-bar/tabbar.wxml-->
<cover-view class="tab-bar">
<cover-view class="tab-bar-border"></cover-view>
<cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
<cover-image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
<cover-view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
</cover-view>
</cover-view>
【5】custom-tab-bar/index.wxss文件代码
/* custom-tab-bar/tabbar.wxss */
.tab-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 48px;
background: white;
display: flex;
padding-bottom: env(safe-area-inset-bottom);
}
.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 cover-image {
width: 27px;
height: 27px;
}
.tab-bar-item cover-view {
font-size: 10px;
}
大功告成!
踩坑指南:
1.custom-tab-bar目录放置根目录下,目录中的文件只能使用index, 当时使用其他名字搞了半天才出来,切记 切记
2.app.js中也必须配置一样的导航信息,如果配置和custom-tab-bar中不一样,或者少 则会出现 无法跳转的情况