根据微信小程序UI库中的iView Weapp封装TabBar

根据微信小程序UI库中的iView Weapp封装TabBar

  • iview Weapp``UI库网址:https://weapp.iviewui.com/docs/guide/start

  • GitHub 下载iView Weapp的代码,将 dist 目录拷贝到自己的项目中。

  • 目录结构:

    在这里插入图片描述

  • app.json中(这里把"style": "v2",删掉,防止兼容等问题)

    {
      "pages":[
        "pages/home/index",
        "pages/doc/index",
        "pages/chat/index",
        "pages/profile/index"
      ],
      "window":{
        "backgroundTextStyle":"light",
        "navigationBarBackgroundColor": "#fff",
        "navigationBarTitleText": "Weixin",
        "navigationBarTextStyle":"black"
      },
      "tabBar": {
        "color":"#1694DD",
        "selectedColor": "#FA314A",
        "backgroundColor":"#f9f9f910",
        "borderStyle":"black",
        "position": "bottom",
        "custom": true,
        "list": [
          {
            "pagePath":"pages/home/index",
            "text":"首页"
          },
          {
            "pagePath":"pages/doc/index",
            "text":"文档"
          },
          {
            "pagePath":"pages/chat/index",
            "text":"消息"
          },
          {
            "pagePath":"pages/profile/index",
            "text":"我的"
          }
        ]
      },
      "usingComponents": {
        "tabbar":"./custom-tab-bar/index"
      },
      "sitemapLocation": "sitemap.json"
    }
    
  • 在自定义组件custom-tab-bar

    • index.json

      {
        "component": true,
        "usingComponents": {
          "i-tab-bar": "../dist/tab-bar/index",
          "i-tab-bar-item": "../dist/tab-bar-item/index",
          "i-icon": "../dist/icon/index"
        }
      }
      
    • index.wxml

      <i-tab-bar bindchange="handleChange" current="{{current}}" color="#ff5c20">
        <i-tab-bar-item wx:for="{{list}}" key="{{index}}" icon="{{item.icon}}" current-icon="{{item.current_icon}}" i-class="active" title="{{item.title}}"></i-tab-bar-item>
      </i-tab-bar>
      
    • index.js

      // custom-tab-bar/index.js
      Component({
        /**
         * 组件的属性列表
         */
        properties: {
      
        },
      
        /**
         * 组件的初始数据
         */
        data: {
          current: 0,
          list: [{
              title: '首页',
              url: '/pages/home/index',
              icon: 'homepage',
              current_icon:'homepage_fill'
            },
            {
              title: '消息',
              url: '/pages/chat/index',
              icon: 'message',
              current_icon:'message_fill'
            },
            {
              title: '文档',
              url: '/pages/doc/index',
              icon: 'document',
              current_icon:'document_fill'
            },
            {
              title: '我的',
              url: '/pages/profile/index',
              icon: 'mine',
              current_icon:'mine_fill'
            },
          ]
        },
      
        /**
         * 组件的方法列表
         */
        methods: {
          handleChange({detail}) {
            // console.log(detail.key);
            this.setData({current:detail.key})
            wx.switchTab({
              url: this.data.list[detail.key].url
            })
          }
        }
      })
      
  • 这时候点击tabbar时有短暂的卡顿现象

    • home页面的index.js

        /**
         * 生命周期函数--监听页面显示
         */
        onShow() {
          this.getTabBar().setData({current:0})
        },
      
    • chat页面的index.js

        /**
         * 生命周期函数--监听页面显示
         */
        onShow() {
          this.getTabBar().setData({current:1})
        },
      
    • doc页面的index.js

        /**
         * 生命周期函数--监听页面显示
         */
        onShow() {
          this.getTabBar().setData({current:2})
        },
      
    • doc页面的index.js

        /**
         * 生命周期函数--监听页面显示
         */
        onShow() {
          this.getTabBar().setData({current:3})
        },
      
  • 页面效果展示:

    在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值