Problem: The tabbar
icon switch needs to be clicked twice to have the selected state.
Reason: There is no copy component function
Custom assembly official website address given: here Wallpaper
in app.json
added:
"tabBar": {
"custom": true,
"color": "#000000",
"selectedColor": "#000000",
"backgroundColor": "#000000",
"list": [{
"pagePath": "page/component/index",
"text": "组件"
}, {
"pagePath": "page/API/index",
"text": "接口"
}]
},
"usingComponents": {
}
Then, list
associate at least two pages in. Obtain the corresponding files in the official sample case code, custom-tab-bar
and image
add the entry file in the code root directory:
custom-tab-bar/index.js
custom-tab-bar/index.json
custom-tab-bar/index.wxml
custom-tab-bar/index.wxss
Then, simply modify the custom-tab-bar/index.js
in to list
make it app.json
consistent with the definition in. Then I thought it was gone, and the problem shown in the title would appear, that is, you need to click twice to switch the icon to the selected state.
solve:
You need to add the following code tabbar
in the js
file of each associated page :
Component({
pageLifetimes: {
show() {
if (typeof this.getTabBar === 'function' &&
this.getTabBar()) {
this.getTabBar().setData({
selected: 0
})
}
}
}
})
Wherein the selected: 0
values need to order according to their specified value. Then, there will be no such phenomenon.
Of course, the default page of this matter is the component. If you need to use it in this way, you need to delete page
the js
file corresponding to each page App
, and then only keep Component
part, and then if you need to click on the function, you need to use the component method, such as:
Component({
pageLifetimes: {
show() {
if (typeof this.getTabBar === 'function' &&
this.getTabBar()) {
this.getTabBar().setData({
selected: 0
})
}
}
},
methods: {
weizu: function(){
console.log(123)
}
}
})
wxml
Event binding can be done in the corresponding file:
<view class="card-range" bindtap="weizu">打卡排行榜</view>
The test found that this method is actually not good and there is flickering.
Since the micro-channel applet done before tabbar
use, remember there are simple ways to consider the custom-tab-bar
deleted files, then app.json
the custom
next revised to false
:
"tabBar": {
"custom": false,
"color": "#000000",
"selectedColor": "#000000",
"backgroundColor": "#000000",
"list": [{
"pagePath": "page/component/index",
"text": "组件"
}, {
"pagePath": "page/API/index",
"text": "接口"
}]
}
Sure enough. No longer consider the way to use custom components to do tabbar
it.