我按照小程序官方文档的自定义tabBar引用了vant组件的tabBar,配置了三个tab页面pageA,pageB,pageC,当我点击tabBar时,只是tab页切换到了对应的页面,但是tabBar还是高亮的之前的标签,我只能再次点击tabBar,才能高亮tab页对应的标签。
这是app.json的配置
这是自定义组件的index.js的代码{
"pages"
: [
"pages/pageA/pageA"
,
"pages/pageC/pageC"
,
"pages/pageB/pageB"
],
"window"
: {
"backgroundTextStyle"
:
"light"
,
"navigationBarBackgroundColor"
:
"#fff"
,
"navigationBarTitleText"
:
"WeChat"
,
"navigationBarTextStyle"
:
"black"
},
"tabBar"
: {
"list"
: [
{
"pagePath"
:
"pages/pageA/pageA"
,
"text"
:
"首页"
},
{
"pagePath"
:
"pages/pageB/pageB"
,
"text"
:
"车主发布"
},
{
"pagePath"
:
"pages/pageC/pageC"
,
"text"
:
"个人中心"
}
],
"custom"
:
true
}
}
这是pageB.js的代码// custom-tab-bar/index.js
// const tabBarList = getApp().globalData.tabBarList;
Component({
/**
* 组件的属性列表
*/
properties: {
},
/**
* 组件的初始数据
*/
data: {
active: 0,
tabBarList: [{
"pagePath"
:
"pages/pageA/pageA"
,
"text"
:
"首页"
},
{
"pagePath"
:
"pages/pageB/pageB"
,
"text"
:
"车主发布"
},
{
"pagePath"
:
"pages/pageC/pageC"
,
"text"
:
"个人中心"
}
]
},
/**
* 组件的方法列表
*/
methods: {
handleTabBarChange(event) {
console.log(
"custom-tab-bar handleTabBarChange event is"
, event,
this
);
wx.switchTab({
url: `/${
this
.data.tabBarList[event.detail].pagePath}`,
success(e) {
console.log(
"custom-tab-bar switchTab success"
, e);
},
fail(event) {
console.log(
"custom-tab-bar switchTab fail"
, event);
},
complete(event) {
console.log(
"custom-tab-bar switchTab complete"
, event);
}
});
this
.setData({
active:event.detail
});
console.log(`tabBarChange done,current active is ${event.detail}`)
}
},
lifetimes: {
attached:
function
() {
// 在组件实例进入页面节点树时执行
console.log(
"attached "
,
this
);
},
detached:
function
() {
// 在组件实例被从页面节点树移除时执行
},
},
})
// pages/pageB/pageB.js
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad:
function
(options) {
console.log(
"PageB onLoad"
,
this
.getTabBar)
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady:
function
() {
console.log(
"PageB onReady"
,
this
.getTabBar)
},
/**
* 生命周期函数--监听页面显示
*/
onShow:
function
() {
console.log(
"PageB onShow"
,
this
,
this
.getTabBar);
if
(
this
.getTabBar) {
this
.getTabBar().setData({
active: 1
})
}
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide:
function
() {
console.log(
"PageB onHide"
,
this
.getTabBar)
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload:
function
() {
console.log(
"PageB onUnload"
,
this
.getTabBar)
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh:
function
() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom:
function
() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage:
function
() {
}
})
自定tabBar的时候,还需要在app.js中设置wx.hideTabBar,隐藏小程序自带的tabBar?
调试基础库版本是 2.9.0
请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
其实使用自定义tabBar的时候,每个tabBar的tab页面就不应该声明为page了,应该声明为component,不然就会导致在生命周期的show方法中获取getTabBar是undefined。