欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

小程序自定义tabBar组件封装

程序员文章站 2022-06-28 14:31:58
本文实例为大家分享了小程序自定义tabbar组件封装的具体代码,供大家参考,具体内容如下1、新建组件:在component下新建一个tabbar2、组件的index.wxml结构如下:

本文实例为大家分享了小程序自定义tabbar组件封装的具体代码,供大家参考,具体内容如下

小程序自定义tabBar组件封装

1、新建组件:在component下新建一个tabbar

2、组件的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="tabchange">
  <cover-image src="{{tabbarindex === index ? item.selectediconpath : item.iconpath}}"></cover-image>
  <cover-view style="color: {{tabbarindex === index ? selectedcolor : color}}">{{item.text}}</cover-view>
 </cover-view>
</cover-view>

3、组件的index.wxss结构如下:

.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 60px;
  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: 28px;
  height: 28px;
  margin-bottom: 2px;
}

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

4、组件的index.js结构如下:

// pages/components/tabbar/index.js
component({
  /**
 1. 组件的属性列表
   */
  options: {
    multipleslots: true //在组件定义时的选项中启用多slot支持
  },
  properties: {
    list: {
      type: array,
      value: []
    },
    selectedcolor:{
      type: string,
      value:''
    },
    color:{
      type: string,
      value:''
    },
  },

  /**
 2. 组件的初始数据
   */
  data: {
    tabbarindex: 0//默认显示第一个tab元素
  },

  lifetimes: {
    attached() {}
  },

  /**
 3. 组件的方法列表
   */
  methods: {
    //组件的点击事件
    tabchange(e) {
      //获取到底部栏元素的下标
      let index = e.currenttarget.dataset.index;
      this.setdata({
        tabbarindex:index,
      })
      //triggerevent获取组件的事件
      //onmyevent 页面传过来的点击事件名称
      this.triggerevent('onmyevent',{
        tabbarindex:index,
      })
    },
  }
})

5、组件的index.json结构如下:

{
  "component": true,
  "usingcomponents": {}
}

6、组件在页面中的使用
7、页面的json代码如下:

{
  "navigationbartitletext": "测试",
  "usingcomponents": {
    "mp-tabbar": "../components/tabbar/index"
  }
}

8、页面的wxml代码如下:

//当选中tab1时页面显示的内容
<view wx:if="{{tabbarindex == 0}}">111111</view>
//当选中tab2时页面显示的内容
<view wx:if="{{tabbarindex == 1}}">222222</view>
//当选中tab3时页面显示的内容
<view wx:if="{{tabbarindex == 2}}">333333</view>
<mp-tabbar list="{{list}}" id='tabcomponent' bind:onmyevent="tabchange"></mp-tabbar>

9、页面的js代码如下:

page({
  data: {
    tabbarindex:0,//默认第一个tab元素
    color: "#555555",
    selectedcolor: "#2ea7e0",
    //底部栏
    list: [{
        "text": "市场",
        "iconpath": "/images/bazaar.png",
        "selectediconpath": "/images/bazaar_selected.png",
      },
      {
        "text": "充值",
        "iconpath": "/images/recharge.png",
        "selectediconpath": "/images/recharge_selected.png",
      }, {
        "text": "车队",
        "iconpath": "/images/market.png",
        "selectediconpath": "/images/market_selected.png",
      }
    ]
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onshow: function () {
    this.tabcomponent = this.selectcomponent('#tabcomponent');
    let selectedcolor = this.data.selectedcolor;
    let color = this.data.color;
    this.tabcomponent.setdata({
      selectedcolor: selectedcolor,
      color:color
   })
   console.log(this.tabcomponent.data.tabbarindex)
  },
  //获取组件传递出来的数据
  tabchange:function(e){
    let index = e.detail.tabbarindex;
    this.setdata({
      tabbarindex:index
    })
    console.log(e.detail.tabbarindex)
  }
})

最终效果如图所示:

小程序自定义tabBar组件封装

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: 小程序 tabBar