小程序自定義tab-bar踩坑實戰(zhàn)記錄
從官方下載代碼
https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html
1、把custom-tab-bar 文件放置 pages同級
修改下 custom-tab-bar 下的 JS文件
Component({
data: {
selected: 0,
color: "#7A7E83",
selectedColor: "#3cc51f",
list: [{
pagePath: "/pages/index/index",
iconPath: "/static/images/ico/home.png",
selectedIconPath: "/static/images/ico/home.png",
text: "A"
}, {
pagePath: "/pages/product/product",
iconPath: "/static/images/ico/home.png",
selectedIconPath: "/static/images/ico/home.png",
text: "B"
},
{
pagePath: "/pages/news/news",
iconPath: "/static/images/ico/home.png",
selectedIconPath: "/static/images/ico/home.png",
text: "C"
},
{
pagePath: "/pages/my/my",
iconPath: "/static/images/ico/home.png",
selectedIconPath: "/static/images/ico/home.png",
text: "D"
},
]
},
attached() {
},
methods: {
switchTab(e) {
const data = e.currentTarget.dataset
const url = data.path
wx.switchTab({url})
// this.setData({ 這部分注銷,到其他頁進(jìn)行賦值
// selected: data.index
// })
}
}
})
2、跳轉(zhuǎn)到指定頁面時,在當(dāng)頁面JS上加上代碼:
onShow: function () { //上面注銷得部分, 在A-D頁面對應(yīng)上, A頁面=0 ,B=1 以此類推
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().setData({
selected: 0 //這里的數(shù)字設(shè)置當(dāng)前頁面在tabbar里對應(yīng)的序列,從0開始
})
}
}
3、在app.json 開啟自定義tab
"tabBar": {
"custom": true, //開啟
"color": "#767676",
"selectedColor": "#004F8A",
"backgroundColor": "#fff",
"borderStyle": "black",
"list": [
4、在app.js 隱藏原生得JS
// app.js
App({
onLaunch() {
wx.hideTabBar(); //隱藏原生得tab bar
// 展示本地存儲能力
const logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
總結(jié)
到此這篇關(guān)于小程序自定義tab-bar踩坑實戰(zhàn)的文章就介紹到這了,更多相關(guān)小程序自定義tab-bar內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript typeof的用法與typeof運(yùn)算符介紹[詳細(xì)]
下面是對于typeof運(yùn)算符的詳細(xì)介紹跟typeof的一些用法,分析,學(xué)習(xí)typeof的朋友,看完了,這篇應(yīng)該能有所收獲。2008-10-10

