微信小程序,自定义tabbar

1. 配置app.json

在app.json中配置"tabBar" 属性

将custom 属性开启,声明是自定义tabBar。color、selectedColor、borderStyle、backgroundColor 在custom为true时并不会渲染组件。list定义tabbar组件切换的页面,在之后使用wx.switchTab方法导航到tabbar组件页面的时候是来查询这个字段的页面。

这里要一旦将page页面定义成tabBar组件页面,就不能使用类似wx.navigateTo、wx.redirectTo方法进行页面跳转。会提示找不到页面。错误提示如下:

2. 创建tabBar组件

在项目根目录创建组件

在微信开发者工具中可以右键文件夹后 >> 点击新建Component 工具会自动帮你建立组件。

配置Index.json

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

配置Index.js

Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },
    /**
     * 组件的初始数据
     */
    data: {
        selected: 0,
        color: "#7A7E83",
        selectedColor: "#2b6bb5",
        list: [{
                "pagePath": "/pages/index/index",
                "iconPath": "/images/foot1.png",
                "selectedIconPath": "/images/foot1-hover.png",
                "text": "首页"
            },
            {
                "pagePath": "/pages/productList/productList",
                "iconPath": "/images/foot2.png",
                "selectedIconPath": "/images/foot2-hover.png",
                "text": "产品"
            },
            {
                "pagePath": "/pages/cart/cart",
                "iconPath": "/images/foot3.png",
                "selectedIconPath": "/images/foot3-hover.png",
                "text": "购物车"
            },
            {
                "pagePath": "/pages/user/user",
                "iconPath": "/images/foot4.png",
                "selectedIconPath": "/images/foot4-hover.png",
                "text": "我的"
            }
        ]
    },

    /**
     * 组件的方法列表
     */
    methods: {
        switchTab(e) {
            const data = e.currentTarget.dataset
            const url = data.path
            if (data.index == 2 || data.index == 3) {
                wx.getStorage({
                    key: 'userInfo',
                    success: function () {
                        wx.switchTab({url})
                    },
                    fail: function () {
                        wx.redirectTo({
                            url: '/pages/login/login',
                        })
                    }
                })
            } else {
                wx.switchTab({url})
            }
        }
    }
})

配置Index.wxml

<cover-view class="tab-bar">
  <cover-view class="tab-bar-border"></cover-view>
  <cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
    <cover-image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
    <cover-view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
  </cover-view>
</cover-view>

配置Index.wxss

.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 48px;
  background: white;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
}

.tab-bar-border {
  background-color: rgba(0, 0, 0, 0.33);
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 1px;
  transform: scaleY(0.5);
}
.tab-bar-item {
  flex: 1;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.tab-bar-item cover-image {
  width: 27px;
  height: 27px;
}

.tab-bar-item cover-view {
  font-size: 10px;
}

最后在每一个tabBar页面的json文件中添加 

"usingComponents": {}

js文件的onShow中添加

if (typeof this.getTabBar === 'function' && this.getTabBar()) {
    this.getTabBar().setData({
      selected: 0
    })
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

swoole~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值