微信小程序如何自定义组件

小程序中一个自定义组件由 json wxml wxss js 4个文件组成。

1 创建自定义组件
首先创建一个components文件夹,用于放置所有自定义的组件。

2 基本配置
mytoast.json(进行自定义组件声明)

{
  "component": true
}

mytoast.wxml

<view class='wx_toast_container' hidden="{{!toastShow}}">
  <view class='wx_toast_text'>{{toastText}}</view>
</view>

mytoast.wxss

.wx_toast_container{
    position: fixed; 
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}
.wx_toast_text{
    background:rgba(0,0,0,0.95);
    color:white;
    text-align:center;
    font-size:34rpx;
    padding:34rpx 50rpx;
    border-radius:20rpx;
    max-width:70%;
    min-width:35%;
    box-sizing:border-box;
    line-height:120%;
}

mytoast.js

Component({  
  options: {  
    multipleSlots: true // 在组件定义时的选项中启用多slot支持  
  },  
  /** 
   * 组件的属性列表 
   */  
  properties: {  
    toastText: {            // 属性名  
      type: String,  
      value: '内容'  
    }  
  },  
  /** 
   * 组件的初始数据 
   */  
  data: {  
    toastShow:false,  
  },  
  /** 
   * 组件的方法列表 
   */  
  methods: {  
    showToast(text,time) {  
      this.setData({  
        toastShow: !this.data.toastShow,  
        toastText: text  
      })  
  
      var that = this  
  
      if (!time){  
        time = 8000  
      }  
  
      setTimeout(function(){  
        that.setData({  
          toastShow: !that.data.toastShow  
        })  
      }, time)  
    }  
  }  
})  

我们要在index.wxml中使用组件的话,首先要在index.json中进行声明

{
  "usingComponents": {
    "mytoast": "/components/mytoast/mytoast"
  }
}

在index.wxml中进行组件的引用

<view>
    <mytoast id="toastedit">{{toastText}}</mytoast>     
    <button type="primary" bindtap="showToast"> showToast! </button>
</view>

index.js的配置

Page({
  onReady: function () {
    //获得mytoast组件
    this.mytoast = this.selectComponent("#mytoast")
  },
  showToast: function () {
    this.mytoast.showToast('我是传过来的toast内容',2000)
  }
})
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
微信小程序是一种基于微信平台的应用程序,它可以在微信中直接运行,无需下载安装。而自定义组件小程序中的一种重要功能,它允许开发者将一些常用的UI元素封装成组件,以便在不同的页面中复用。 自定义组件具有以下特点: 1. 组件是由wxml、wxss和js文件组成,可以独立定义样式和逻辑。 2. 组件可以接受外部传入的数据,通过属性进行配置。 3. 组件可以触发事件,向外部传递消息。 4. 组件可以包含子组件,形成组件的嵌套结构。 使用自定义组件的步骤如下: 1. 在小程序项目中创建一个新的文件夹,用于存放自定义组件的相关文件。 2. 在该文件夹中创建一个wxml文件,定义组件的结构。 3. 在同一文件夹中创建一个wxss文件,定义组件的样式。 4. 在同一文件夹中创建一个js文件,定义组件的逻辑。 5. 在需要使用该组件的页面中引入组件,并在wxml中使用组件标签。 例如,我们创建一个名为"custom-component"的自定义组件,其文件结构如下: ``` custom-component/ ├── custom-component.wxml ├── custom-component.wxss └── custom-component.js ``` 在custom-component.wxml中定义组件的结构,例如: ```html <view class="custom-component"> <text>{{text}}</text> <button bindtap="handleClick">点击按钮</button> </view> ``` 在custom-component.wxss中定义组件的样式,例如: ```css .custom-component { background-color: #f5f5f5; padding: 10px; } ``` 在custom-component.js中定义组件的逻辑,例如: ```javascript Component({ properties: { text: { type: String, value: '默认文本' } }, methods: { handleClick() { this.triggerEvent('click', { message: '按钮被点击了' }); } } }) ``` 在需要使用该组件的页面中引入组件,并在wxml中使用组件标签,例如: ```html <custom-component text="Hello World" bind:click="handleCustomComponentClick"></custom-component> ``` 以上就是微信小程序自定义组件的简单介绍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

敲起来blingbling

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

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

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

打赏作者

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

抵扣说明:

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

余额充值