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

简单的自定义组件

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

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

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

step1:创建文件夹以及文件

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

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

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

创建之后的目录结构为:

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

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

step2:组件的基本搭建

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

<view bindtap="callPhone"> <view>{{phone}}</view> <view>了解最新信息,免费咨询</view> <view></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+0p5VinFhed2DP7C0zdPiGuyfJucpVzb99UTYgKZlQSuJKB88GYNsCPgV4QXiGiVQe5M9ji/ag7/fazo8gJaIvWn3GsS0y4l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JX6MtxqGLFriFc6ACTVOZjF7TBx6HQaygTyA7RDu9qxUR3hQtdrdKDme/90zguVlgVwvnSPy0Z0kKnvvQHLCVFoSdklsANeoCHdR8FrC4yTar7KAuKOFGD5d+ozyLJvqO6++hwnAf63FKcr5GE2rR9bmXRnrgr1nlMUp/bA5os12KuchZf8j63qQ5poaPyGD7dgamH9e4mzBCsenervt2qf/dA727qJHCnJOwTnB+9u/8PGdpORDiwhy4AAAAASUVORK5CYII=); }

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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/52e70881cff3ae129faf7bd7e55d9eca.html