在onshow生命周期函数里初始active的值,用来对应每个页面切换之后展示对应的图标
以这两个页面为例子
(1)tabBar=>index.js
data: {
active: 0,
list: [
{
pagePath: "/pages/index/index",
text: "工作台",
icon: "home-o",
},
{
pagePath: "/pages/logs/logs",
text: "我的",
icon:"user-o",
},
]
},
methods: {
onChange(e) {
this.setData({ active: e.detail });
this.setData({ active: e.detail });
wx.switchTab({
url: this.data.list[e.detail].pagePath
});
},
init() {
const page = getCurrentPages().pop();
this.setData({
active: this.data.list.findIndex(item => item.pagePath === `/${page.route}`)
});
},
}
(2)index页面
onShow() {
this.getTabBar().init({
active: 0
})
},
(3)log页面
onShow() {
this.getTabBar().init({
active: 1
})
},