当前位置:首页 >> 网络编程

详解微信小程序自定义组件的实现及数据交互

简单的自定义组件

本文的使用场景是:多个页面都需要用到拨打电话功能,为减少代码量、便于维护,所以自定义了一个组件,每个页面直接调用即可。基本实现下图样式:

详解微信小程序自定义组件的实现及数据交互

下面我们来一步步的实现它:

step1:创建文件夹以及文件

首先创建一个components(名字可自取)文件夹,用于放置所有自定义的组件,

里面可以用右键自动创建的方式,新建一个Component组件,例如:

详解微信小程序自定义组件的实现及数据交互

创建之后的目录结构为:

详解微信小程序自定义组件的实现及数据交互

其中callphone是我们本次要实现的拨打电话组件。

step2:组件的基本搭建

在callphone.wxml中:添加组件结构:

<view class="call" bindtap="callPhone">
 <view class="phone">{{phone}}</view>
 <view>了解最新信息,免费咨询</view>
 <view class="callIcon"></view>
</view>

在callphone.wxss中:添加组件样式:

.call {
 display: block;
 padding: 18rpx 30rpx;
 width: 100%;
 color: #999;
 font-size: 24rpx;
 box-sizing: border-box;
 background: #ecf9e8;
 position: relative;
 border-radius: 10rpx;
}
.call .phone {
 color: #333;
 font-size: 40rpx;
}
.call .callIcon {
 width: 77rpx;
 height: 77rpx;
 position: absolute;
 top: 50%;
 margin-top: -38rpx;
 right: 30rpx;
 background-size: cover;
 background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAE0AAABNCAYAAADjCemwAAALaUlEQVR4Xu1ce3BU1Rn/fecmmxCIDwoqOI5PEmDvwlh8dQwkuyAx2Y3TqmDt+NbOtIpax9Y61VpatdM60hkr7Yzjo1U6rYJYdbOLEbI3RHwWamFvwKC2tpZgCxUhEpNN9nydc7MJm7DJ3t3cJYuz58/kO9/5zm/PPed7E8Zp1L82s4JixXMZslIQVUhghgCOZ3A5QOUAygB0AdxJoE4J7BPA+5J5J0G0s6t3a3j+ezvHQ3w6UovWtZ49VfT1NoDgBbMPoOljX5s7QBQBw5BFxcF1C97dM3ae6TnkFLQa47TSiVTewMxXg3ExAcWDIhF2E/A2g9qJ0B6XcicVF+0p5VinFhed2D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CrlAblUVtlPUoFKrwUmo4hXpP9fj1l2Y7U++pYC5UFh9+2NKeNDWlUMM+FDhboFkPg+qWIF2toEK3BNugDQJX6MtxqGLFriFc6ACTVOZjF7TBx6HQaygTyA7RDu9qxUR3hQtdrdKDme/90zguVlgVwvnSPy0Z0kKnvvQHLCVFoSdklsANeoCHdR8FrC4yTar7KAuKOFGD5d+ozyLJvqO6++hwnAf63FKcr5GE2rR9bmXRnrgr1nlMUp/bA5os12KuchZf8j63qQ5poaPyGD7dgamH9e4mzBCsenervt2qf/dA727qJHCnJOwTnB+9u/8PGdpORDiwhy4AAAAASUVORK5CYII=);
}

在callphone.js中:添加组件逻辑:

Component({
 /**
  * 组件的属性列表
  */
 properties: {

 },
 /**
  * 组件的初始数据
  */
 data: {
  phone: '400-010-9797'
 },
 /**
  * 组件的方法列表
  */
 methods: {
  callPhone() {
   wx.makePhoneCall({
    phoneNumber: this.data.phone
   })
  }
 }
})

step3:使用组件

我们要在index.wxml中使用该组件的话,要在index.json中进行声明:(其中call-phone是组件的名称,可以自取)

{
 "usingComponents": {
  "call-phone": "/components/CallPhone/callphone"
 }
}

在index.wxml中引用组件:

<call-phone></call-phone>

完成以上几个步骤,一个简单的小程序自定义组件就完成了,运行结果如下:

详解微信小程序自定义组件的实现及数据交互

Page与Component数据交互

上面的例子中可以看到,电话号码是在组件中的data中设置的,也就是每个页面都是固定写死的;但是如果要实现每个界面的号码不一样的话,此时就涉及到page往组件传值的问题,可以这么修改:

1、page > component

方法1:page在引用组件时能通过属性值设置,callphone.js在properties获取:

<!--index.wxml-->
<call-phone phone="400-010-9797"></call-phone>
// components/CallPhone/callphone.js
Component({
 properties: {
  phone: String //简写
  /* 
  phone: {
   type: String, //类型,目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
   value: '', //初始值
   observer: function(newVal, oldVal) {}
  }
  */
 },
 attached: function() {
  console.log("this.properties.phone:" + this.properties.phone); //控制台打印:"400-010-9797"
 },
 //...
})

方法2:page在引用组件时通data-xxx设置,callphone.js在this.dataset获取:

<!--index.wxml-->
<call-phone data-tel="400-010-9797" ></call-phone>
// components/CallPhone/callphone.js
Component({
 attached: function() {
  console.log("this.dataset.tel:" + this.dataset.tel); //控制台打印:"400-010-9797"
  // 设置properties值用setData()
  this.setData({
   phone: this.dataset.tel
  });
 },
 //...
})

2、component > page

组件中的变量要传到page页面,可以通过事件触发this.triggerEvent()来实行:

// components/CallPhone/callphone.js
Component({
 methods: {
  callPhone() {
   var myEventDetail = {
    msg: '来自component的信息',
    tel: '400-010-9797'
   } // detail对象,提供给事件监听函数
   var myEventOption = {} // 触发事件的选项
   this.triggerEvent('myevent', myEventDetail, myEventOption)
  }
 }
})
<!--index.wxml-->
<!--"bindmyevent"属性与上面"myevent"对应,当自定义组件触发"myevent"事件时,调用"call"方法 -->
<call-phone bindmyevent="call"></call-phone>
//index.js
Page({
 call: function(e) {
  console.log(e.detail.msg);//控制台打印:"来自component的信息"
  wx.makePhoneCall({
   phoneNumber: e.detail.tel
  })
 }
})

总结

以上所述是小编给大家介绍的微信小程序自定义组件的实现及数据交互,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!