通过自定义组件实现动态tab-bar,并且防止tab切换时出现跳动问题
将tab页面写成一个组件(不知道有啥副作用,不过目前似乎可行)
小程序ui框架(Vant Weapp)
- index(wxml)
<block>
<view wx:if="{{active == '待办通知'}}">
<todo-notice></todo-notice>
</view>
<view wx:if="{{active == '入户检查'}}">
<indoor-check></indoor-check>
</view>
<view wx:if="{{active == '电话回访'}}">
<text>电话回访</text>
</view>
<view wx:if="{{active == '督促整改'}}">
<text>督促整改</text>
</view>
<view wx:if="{{active == '我的'}}">
<text>我的</text>
</view>
</block>
<van-tabbar active="{{ active }}" bind:change="onChange" placeholder="true" safe-area-inset-bottom>
<van-tabbar-item wx:for="{{list}}" wx:key="index" name="{{item.text}}" wx:if="{{item.isShow}}">
<image slot="icon" src="{{ item.iconPath }}" mode="aspectFit" style="width: 28px; height: 28px;" />
<image slot="icon-active" src="{{ item.selectedIconPath }}" mode="aspectFit"
style="width: 28px; height: 28px;" />
{{item.text}}
</van-tabbar-item>
</van-tabbar>
- index(index.js)
const app = getApp()
Page({
data: {
active: '',
title: '首页',
list: []
},
onLoad() {
this.setData({ //动态tab-bar数据格式
list: [{
index: 0,
pagePath: "/pages/index/index",
text: "待办通知",
iconPath: "../../images/tab_bar/dbtz.png",
selectedIconPath: "../../images/tab_bar/dbtz-active.png",
isShow: true,
},
{
index: 1,
pagePath: "/pages/household/household",
text: "入户检查",
iconPath: "../../images/tab_bar/rhjc.png",
selectedIconPath: "../../images/tab_bar/rhjc-active.png",
isShow: true
},
{
index: 2,
pagePath: "/pages/phoneback/phoneback",
text: "电话回访",
iconPath: "../../images/tab_bar/callphone.png",
selectedIconPath: "../../images/tab_bar/callphone-active.png",
isShow: true
},
{
index: 3,
pagePath: "/pages/rectification/rectification",
text: "督促整改",
iconPath: "../../images/tab_bar/dczg.png",
selectedIconPath: "../../images/tab_bar/dczg-active.png",
isShow: true
},
{
index: 4,
pagePath: "/pages/mine/mine",
text: "我的",
iconPath: "../../images/tab_bar/mine.png",
selectedIconPath: "../../images/tab_bar/mine-active.png",
isShow: true
}
]
})
try { // 刚进页面时判断第一个tab
this.data.list.forEach((item) => {
if (item.isShow) {
this.setData({
active: item.text,
// title: item.text
})
throw ('循环停止')
}
})
} catch (e) {
console.log(e)
}
},
onChange(event) {
console.log(event)
this.setData({
active: event.detail,
// title: event.detail
})
}
})
3.index(index.json)
{
"usingComponents": {
"van-tabbar": "@vant/weapp/tabbar/index",
"van-tabbar-item": "@vant/weapp/tabbar-item/index",
"van-nav-bar": "@vant/weapp/nav-bar/index",
"todo-notice": "../../components/todoNotice/todoNotice",
"indoor-check": "../../components/indoorCheck/indoorCheck"
},
"enablePullDownRefresh": false,
"navigationStyle": "custom"
}