小程序自定义tabbar,并实现点击事件拦截

需求:有首页和我的两个tabbar页面,点击我的时判断是否登录,如果未登录跳转到登录页,如果已登录进入到我的页面

第一部分:配置app.json:

{
    "pages": [
        "pages/index/index",
        "pages/center/center",
        "pages/login/login"
    ],
    "window": {
        "backgroundTextStyle": "light",
        "navigationBarBackgroundColor": "#fff",
        "navigationBarTitleText": "WeChat",
        "navigationBarTextStyle": "black"
    },
    "tabBar": {
        "custom": true,
        "list": [
            {
                "pagePath": "pages/index/index",
                "text": "首页",
                "iconPath": "images/tabBarIcon-index.png",
                "selectedIconPath": "images/tabBarIcon-index-HL.png"
            },
            {
                "pagePath": "pages/center/center",
                "text": "我的",
                "iconPath": "images/tabBarIcon-center.png",
                "selectedIconPath": "images/tabBarIcon-center-HL.png"
            }
        ]
    },
    "sitemapLocation": "sitemap.json"
}

第二部分:编写tabBar代码:
在根目录新建custom-tab-bar文件夹,并在文件夹中创建文件:
custom-tab-bar/index.js
custom-tab-bar/index.json
custom-tab-bar/index.wxml
custom-tab-bar/index.wxss

custom-tab-bar/index.js中的代码

const app = getApp()
Component({
    data: {
        selected: 0,
        color: "#666",
        selectedColor: "#02c260",
        list: [{
            pagePath: "/pages/index/index",
            iconPath: "/images/tabBarIcon-index.png",
            selectedIconPath: "/images/tabBarIcon-index-HL.png",
            text: "首页"
        }, {
            pagePath: "/pages/center/center",
            iconPath: "/images/tabBarIcon-center.png",
            selectedIconPath: "/images/tabBarIcon-center-HL.png",
            text: "我的"
        }]
    },
    attached() {},
    methods: {
        switchTab(e) {
            const data = e.currentTarget.dataset
            const url = data.path
            
            if (url === '/pages/center/center' && !app.globalData.isLogin) {
                wx.navigateTo({
                  url: '/pages/login/login'	//可以带参数,在登录页面接收
                })
                return;
            }
            this.setData({
                selected: data.index
            })
            wx.switchTab({
                url
            })

        }
    }
})

custom-tab-bar/index.json中的代码

{
    "component": true
}

custom-tab-bar/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>

custom-tab-bar/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;
  }

第三部分:业务页面代码:

pages/index/index.js中

  Page({
    data: {
    },
    onLoad: function (options) {
    },
    onShow() {
      if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
          selected: 0
        })
      }
    }
  })

pages/center/center.js中

Page({
    data: {
    },
    onLoad: function (options) {
    },
    onShow() {
      if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
          selected: 1
        })
      }
    }
})

pages/logon/login.js中执行登录

const app = getApp()
Page({
    login () {
        app.globalData.isLogin = true;
        wx.showToast({title: '登录成功'})
        wx.navigateBack()
    },
    data: {
    },
    onLoad: function (options) {
    }
})
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值