微信小程序动态修改tabBar数据修改后,跳转报错
Unhandled promise rejection {errMsg: "switchTab:fail can not switch to no-tabBar page
因为在custom-tab-bar中页面跳转我是用wx.switchTab来跳转的,在点击多端登录后修改tabBar数据后跳转,为什么会报错?难道修改数据后新增的list页面数据不算tabBar页面吗?
然后就是修改完tabBar数据刷新的话,索引为0的tabBar页面是默认之前数据索引为0的首页,这是为什么啊?在点击tabBar后,页面会短暂的白屏闪烁,然后变成修改的tabBar
custom-tab-bar:
<cover-view class="root">
<cover-view wx:for="{{tabBar.list}}" data-index="{{index}}" data-url="{{item.pagePath}}" class="tabView" bindtap="btnTab" wx:key="index">
<cover-image class="imgView" src="{{index === tabIndex ? item.selectedIconPath : item.iconPath}}"></cover-image>
<cover-view wx:if="{{index == tabIndex}}" style="padding: 5rpx;color: #DC351F;">{{item.text}}</cover-view>
<cover-view wx:else="{{index != tabIndex}}" style="padding: 5rpx;color: #000;">{{item.text}}</cover-view>
</cover-view>
</cover-view>
// custom-tab-bar/index.js
const datas = {
custom: true,
color: "#000",
selectedColor: "#DC351F",
list: [
{
pagePath: "../index/index",
text: "首页",
iconPath: "../icons/shouye.png",
selectedIconPath: "../icons/shouye-sele.png"
},
{
pagePath: "../category/index",
text: "分类",
iconPath: "../icons/fenlei.png",
selectedIconPath: "../icons/fenlei-sele.png"
},
{
pagePath: "../cart/index",
text: "购物车",
iconPath: "../icons/gouwuche.png",
selectedIconPath: "../icons/gouwuche-sele.png"
},
{
pagePath: "../user/index",
text: "我的",
iconPath: "../icons/wode.png",
selectedIconPath: "../icons/wode-sele.png"
}
]
}
Page({
/**
* 页面的初始数据
*/
data: {
tabIndex:0,
tabBar:wx.getStorageSync('tabBar') || datas,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
console.log('tabBar',this.data.tabBar);
},
//
async btnTab(e) {
this.setData({
tabIndex:e.currentTarget.dataset.index
})
wx.switchTab({
url: `${e.currentTarget.dataset.url}`,
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})
这是多端登录触发修改tabBar数据的代码:
// 点击配送端登录
async LoginCutEndFun(){
let datas = {
custom: true,
color: "#000",
selectedColor: "#DC351F",
list: [
{
pagePath: "../order/index",
text: "订单",
iconPath: "../icons/order.png",
selectedIconPath: "../icons/order-sele.png"
},
{
pagePath: "../user/index",
text: "我的",
iconPath: "../icons/wode.png",
selectedIconPath: "../icons/wode-sele.png"
}
]
}
const tab = this.getTabBar()
tab.setData({
tabBar:datas
})
wx.setStorageSync('tabBar', datas)
tab.setData({
tabIndex: 1
})
},
大锅们,帮忙解答一下,谢谢