亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

這篇文章主要為大家詳細介紹了微信小程序自定義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/[email protected]",
        indexSelectImg: "../static/images/tabBar/[email protected]",
        aboutUsImg: "../static/images/tabBar/[email protected]",
        aboutUsSelectImg: "../static/images/tabBar/[email protected]",
        _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組件
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定