一、创建自定义组件
类似于页面,一个自定义组件由 json wxml wxss js 4个文件组成。要编写一个自定义组件,首先需要在 json 文件中进行自定义组件声明(将 component 字段设为 true 可这一组文件设为自定义组件)
二、使用自定义组件
使用已注册的自定义组件前,首先要在页面的 json 文件中进行引用声明。此时需要提供每个自定义组件的标签名和对应的自定义组件文件路径
1 2 3 4 5
| { "usingComponents": { "component-tag-name": "path/to/the/custom/component" } }
|
这样,在页面的 wxml 中就可以像使用基础组件一样使用自定义组件。节点名即自定义组件的标签名,节点属性即传递给组件的属性值。
三、组件模版
- 在组件模板中可以提供一个
节点,用于承载组件引用时提供的子节点
1 2 3 4 5
| <view class="wrapper"> <view>这里是组件的内部节点view> <slot>slot> view>
|
1 2 3 4 5 6 7
| <view> <component-tag-name> <view>这里是插入到组件slot中的内容view> component-tag-name> view>
|
模版数据绑定
可以使用数据绑定,这样就可以向子组件的属性传递动态数据
1 2 3 4 5 6 7
| <view> <component-tag-name prop-a="{{dataFieldA}}" prop-b="{{dataFieldB}}"> <view>这里是插入到组件slot中的内容view> component-tag-name> view>
|
组件wxml的slot
- 在组件的
wxml中可以包含 slot 节点,用于承载组件使用者提供的wxml结构。
- 默认情况下,一个组件的
wxml中只能有一个slot。需要使用多slot时,可以在组件js中声明启用
1 2 3 4 5 6 7
| Component({ options: { multipleSlots: true }, properties: { }, methods: { } })
|
此时,可以在这个组件的wxml中使用多个slot,以不同的 name 来区分
1 2 3 4 5 6
| <view class="wrapper"> <slot name="before">slot> <view>这里是组件的内部细节view> <slot name="after">slot> view>
|
使用时,用 slot 属性来将节点插入到不同的slot上
1 2 3 4 5 6 7 8 9
| <view> <component-tag-name> <view slot="before">这里是插入到组件slot name="before"中的内容view> <view slot="after">这里是插入到组件slot name="after"中的内容view> component-tag-name> view>
|
四、Component构造器
Component构造器可用于定义组件,调用Component构造器时可以指定组件的属性、数据、方法等
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| Component({
behaviors: [],
properties: { myProperty: { type: String, value: '', observer: function(newVal, oldVal, changedPath) { } }, myProperty2: String }, data: {},
attached: function(){}, moved: function(){}, detached: function(){},
methods: { onMyButtonTap: function(){ this.setData({ }) }, _myPrivateMethod: function(){ this.setData({ 'A[0].B': 'myPrivateData' }) }, _propertyChange: function(newVal, oldVal) {
} }
})
|
- 在 properties 定义段中,属性名采用驼峰写法(propertyName);
- 在 wxml 中,指定属性值时则对应使用连字符写法(
component-tag-name property-name="attr value"),应用于数据绑定时采用驼峰写法(attr="")
五、组件间通信与事件
组件间通信
组件间的基本通信方式有以下几种。
- WXML 数据绑定:用于父组件向子组件的指定属性设置数据
- 事件:用于子组件向父组件传递数据,可以传递任意数据。
- 如果以上两种方式不足以满足需要,父组件还可以通过
this.selectComponent 方法获取子组件实例对象,这样就可以直接访问组件的任意数据和方法
监听事件
- 自定义组件可以触发任意的事件,引用组件的页面可以监听这些事件
- 监听自定义组件事件的方法与监听基础组件事件的方法完全一致
1 2 3 4
| <component-tag-name bindmyevent="onMyEvent" />
<component-tag-name bind:myevent="onMyEvent" />
|
1 2 3 4 5
| Page({ onMyEvent: function(e){ e.detail } })
|
触发事件
自定义组件触发事件时,需要使用 triggerEvent 方法,指定事件名、detail对象和事件选项
1 2
| <button bindtap="onTap">点击这个按钮将触发“myevent”事件button>
|
1 2 3 4 5 6 7 8 9 10
| Component({ properties: {} methods: { onTap: function(){ var myEventDetail = {} var myEventOption = {} this.triggerEvent('myevent', myEventDetail, myEventOption) } } })
|
六、自定义组件案例
实现一个弹框

popup.wxml
1 2 3 4 5 6 7 8 9 10
| <view class="wx-popup" hidden="{{flag}}"> <view class='popup-container'> <view class="wx-popup-title">{{title}}view> <view class="wx-popup-con">{{content}}view> <view class="wx-popup-btn"> <text class="btn-no" bindtap='_error'>{{btn_no}}text> <text class="btn-ok" bindtap='_success'>{{btn_ok}}text> view> view> view>
|
popup.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| Component({ options: { multipleSlots: true },
properties: { title: { type: String, value: '标题' }, content: { type: String, value: '内容' }, btn_no: { type: String, value: '取消' }, btn_ok: { type: String, value: '确定' } },
data: { flag: true, },
methods: { hidePopup: function () { this.setData({ flag: !this.data.flag }) }, showPopup () { this.setData({ flag: !this.data.flag }) },
_error () { this.triggerEvent("error") }, _success () { this.triggerEvent("success"); } } })
|
在首页用这个组件需要配置一下,首先建一个名为index.json的文件,里边配置”usingComponents”,就是需要引入到首页
1 2 3 4 5
| { "usingComponents": { "popup": "/component/popup/popup" } }
|
引入到首页
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <view class="container"> <view class="userinfo"> <button bindtap="showPopup"> 点我 button> view> <popup id='popup' title='小组件' content='学会了吗' btn_no='没有' btn_ok='学会了' bind:error="_error" bind:success="_success"> popup> view>
|
配置index.js操作点击事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
const app = getApp() Page({ onReady: function () { this.popup = this.selectComponent("#popup"); }, showPopup() { this.popup.showPopup(); }, _error() { console.log('你点击了取消'); this.popup.hidePopup(); }, _success() { console.log('你点击了确定'); this.popup.hidePopup(); } })
|