溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

微信小程序如何自定義tabbar組件

發布時間:2021-03-15 10:39:43 來源:億速云 閱讀:405 作者:TREX 欄目:開發技術

這篇文章主要講解了“微信小程序如何自定義tabbar組件”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“微信小程序如何自定義tabbar組件”吧!

由于項目需求,必須自己寫組件:

第一步:在App.json中配置tabBar,自定也組件也必須配置,"custom": true,list里配置所有的tabbar頁面。

 "tabBar": {
 "custom": true,
 "color": "red",
 "selectedColor": "#3b81d7",
 "backgroundColor": "#000000",
 "list": [{
 "pagePath": "pages/Role/InsureIndex/index",
 "text": "首頁"
 }, {
 "pagePath": "pages/Role/MineIndex/index",
 "text": "首頁"
 }, {
 "pagePath": "pages/index/userInfo/userInfo",
 "text": "我的"
 }]
 },

第二步:在pages的同級目錄新建組件,文件夾名字:custom-tab-bar,自定義組件文件名為index。組件代碼如下,應該都能看懂。

index.js

Component({
 properties: {},
 
 data: {
 indexImg: "../static/images/tabBar/tab_icon_home_nor@2x.png",
 indexSelectImg: "../static/images/tabBar/tab_icon_home_sel@2x.png",
 aboutUsImg: "../static/images/tabBar/tab_icon_user_nor@2x.png",
 aboutUsSelectImg: "../static/images/tabBar/tab_icon_user_sel@2x.png",
 _tabbat: null,
 iPhoneX: false,
 urls: ['/pages/Role/InsureIndex/index',
 '/pages/index/userInfo/userInfo'
 ]
 },
 attached() {
 var self = this
//此為業務代碼,可不看
 wx.getStorage({
 key: 'userInfo',
 success: function (res) {
 const {
  userRoleCode
 } = res.data
 if (userRoleCode == '50' || userRoleCode == '70') {
  self.setData({
  ["urls[0]"]: '/pages/Role/MineIndex/index'
  })
 } else if (userRoleCode == '30' || userRoleCode == '35' || userRoleCode == '40') {
  self.setData({
  ["urls[0]"]: '/pages/Role/InsureIndex/index'
  })
 }
 }
 })
 wx.getSystemInfo({
 success(res) {
 console.log(res.model)
 if (res.model.indexOf('iPhone X') >= 0) {
  self.setData({
  iPhoneX: true
  })
 }
 }
 })
 },
 /**
 * 組件的方法列表
 */
 methods: {
 switchTap: function (e) {
 var self = this
 var index = e.currentTarget.dataset.index;
 var urls = self.data.urls
 wx.switchTab({
 url: urls[index],
 })
 }
 }
})

index.wxml

<div class="_tabbar {{iPhoneX?'_iPhoneX':''}}">
 <div class="titem {{_tabbat==0?'tCdk':''}}" data-index="0" bind:tap="switchTap">
 <image src="{{_tabbat==0?indexSelectImg:indexImg}}" />
 <b>首頁</b>
 </div>
 <div class="titem {{_tabbat==1?'tCdk':''}}" data-index="1" bind:tap="switchTap">
 <image src="{{_tabbat==1?aboutUsSelectImg:aboutUsImg}}" />
 <b>我的</b>
 </div>
</div>

index.wxss

._tabbar {
 width: 100%;
 height: 120rpx;
 display: flex;
 align-items: center;
 background: #fff;
 font-size: 26rpx;
 color: #999999;
 box-shadow: 0px -7rpx 13rpx 0px rgba(193, 185, 204, 0.13);
}
 
._tabbar .titem {
 text-align: center;
 width: 50%;
}
 
._tabbar .titem image {
 display: block;
 margin: auto;
 width: 48rpx;
 height: 48rpx;
 margin-bottom: 10rpx;
}
 
._tabbar .tCdk {
 color: #37ADFE;
}
 
._iPhoneX {
 height: 148rpx;
}

index.json

{
 "component": true,
 "usingComponents": {}
}

以上為組件代碼,點擊tabbar跳轉頁面時,會重新加載tabbar組件,導致選中樣式一直是默認的,因此需要在用到tabbar的頁面的js文件中做如下操作:(以 “首頁” 頁面為例)

onShow: function () {
 this.getTabBar().setData({
 _tabbat: 0
 })
 },

以上就已經完成了,但是看網上大家說會出現兩個tabBar,我這邊是沒出現(一個自定義,一個自帶的),如果出現的話,在app.js中的onLaunch函數中加入 wx.hideTabBar() , 隱藏自帶的tabbar就可以了。

感謝各位的閱讀,以上就是“微信小程序如何自定義tabbar組件”的內容了,經過本文的學習后,相信大家對微信小程序如何自定義tabbar組件這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女