微信小程序封装tab组件--自定义组件

微信小程序封装组件

1,准备子组件

//新建components文件夹,再建tab文件如下
// (1)tab.wxml
<view class="tabBox">
  <view wx:for="{{tabList}}" wx:key='index' class="tab">
    <view class='{{item.id==tabId?"active":""}}' 
    bindtap="_tabChange" 
    data-id='{{item.id}}'>
    {{item.title}}
  </view>
  </view>
</view>
// (2)tab.js
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  //接收父组件的参数
  properties: {
    tabList: {            
      type: Array,      
      value: []     
    },
    tabId: {            
      type: String,      
      value: '0'    
    },
    
  },
//组件的初始数据
  data: {
  
  },
//组件的方法列表
  methods: {
    _tabChange(e){
      let tabId =  e.currentTarget.dataset.id
      //更新子组件内
      this.setData({
      	 tabId :tabId
      })
     //调用父组件自定义方法,传参
      this.triggerEvent('onTab', tabId)
    },
  }
})
// (3)tab.wxss
.tabBox{
  display: flex;
} 
.tab{
  width: 200rpx;
  margin-top: 20rpx;
  text-align:center;
}
.tabBox .active{
  color: red;
  font-size: 32rpx;
  font-weight: 700;
  border-bottom: 4rpx solid red;
}
### 2,在父组件中使用
```javascript
// (1)index.json引入
"usingComponents": {
    "tab":"/components/tab/tab"
  },
// (2)index.wxml使用
<tab tabList='{{tabList}}' tabId='{{tabId}}' bind:onTab='onTab'></tab>
//自定义方法 = 方法名
// (3)index.js
// pages/eg-flex-one/index.js
Page({
  data: {
    tabList:[
      {title:'美食',id:'0'},
      {title:'风景',id:'1'},
      {title:'生活',id:'2'}
    ],
    tabId:'0',
 }

  onTab(e){
   //得到子组件传来的参数
    let tabId = e.detail
    this.setData({
      tabId:tabId
    })
    console.log(tabId)
  },
 })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序提供的组件中,Grid(网格)是一种非常强大和灵活的布局组件,可以实现多种不同的网格视图。其中一种常见的应用场景是行和列同时滑动,从而使用户可以快速浏览并选择不同的内容。 要实现这种功能,首先需要在 Grid 组件中设置 scroll-x 和 scroll-y 两个属性为 true,以允许同时滑动水平和垂直方向。同时,还需要设置每个网格项(即每个单元格)的宽度和高度,以确保它们能够按照预期的方式排列在网格中。 具体的实现方法可以分为以下几个步骤: 1. 在 wxml 文件中添加 Grid 组件,并设置 scroll-x 和 scroll-y 属性为 true。 ```html <view class="grid-container"> <grid scroll-x scroll-y> <!-- 网格项内容 --> </grid> </view> ``` 2. 在样式表中设置网格容器的高度和宽度,以及网格项的宽度和高度。 ```css .grid-container { width: 100%; height: 500rpx; } .grid-item { width: 33.3%; height: 200rpx; } ``` 3. 在 js 文件中动态设置网格项的内容和数量。 ```js Page({ data: { gridData: [] }, onLoad: function () { // 从服务器获取网格项数据 // ... // 将数据存储到 data 中 this.setData({ gridData: [ { text: '网格项1' }, { text: '网格项2' }, { text: '网格项3' }, { text: '网格项4' }, { text: '网格项5' }, // ... ] }); } }) ``` 4. 在 wxml 文件中使用 wx:for 循环遍历网格数据,并将网格项的文本内容显示出来。 ```html <view class="grid-container"> <grid scroll-x scroll-y> <block wx:for="{{ gridData }}" wx:key="{{ index }}"> <grid-item class="grid-item">{{ item.text }}</grid-item> </block> </grid> </view> ``` 综上所述,通过上述步骤,即可实现微信小程序组件 Grid 的行和列同时滑动的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值