1# 自定义组件使用说明 2<!--Kit: ArkUI--> 3<!--Subsystem: ArkUI--> 4<!--Owner: @seaside_wu1--> 5<!--Designer: @shiyu_huang--> 6<!--Tester: @TerryTsao--> 7<!--Adviser: @HelloCrease--> 8 9 10> **说明:** 11> 12> 从API Version 8 开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 13 14 15自定义组件是用户根据业务需求,将已有的组件组合,封装成的新组件,可以在工程中多次调用,提高代码的可读性。自定义组件通过element引入到宿主页面,使用方法: 16 17```html 18<element name='comp' src='../../common/component/comp.hml'></element> 19<div> 20 <comp prop1='xxxx' @child1="bindParentVmMethod"></comp> 21</div> 22``` 23 24- name属性指自定义组件名称(非必填),组件名称对大小写不敏感,默认使用小写。src属性指自定义组件hml文件路径(必填),若没有设置name属性,则默认使用hml文件名作为组件名。 25 26- 事件绑定:自定义组件中绑定子组件事件使用(on|\@)child1语法,子组件中通过{action:"proxy", method: "eventName"}触发事件并进行传值,父组件执行bindParentVmMethod方法并接收子组件传递的参数。 27 28## 自定义组件配置文件标签 29 30| 属性 | 类型 | 描述 | 31| -------- | -------- | -------- | 32| data | Object | 页面的数据模型,类型是对象。属性名不能以$或_开头,不要使用保留字for, if, show, tid。 | 33| props | Array/Object | props用于组件之间的通信,可以通过<tag xxxx='value'>方式传递给组件;props名称必须用小写,不能以$或_开头,不要使用保留字for, if, show, tid。目前props的数据类型不支持Function。 | 34 35 36## 添加自定义事件 37 38自定义组件内支持自定义事件,该事件的标识需要action类型指定为proxy,事件名则通过method指定。使用该自定义组件的卡片页面可以通过该事件名注册相应的事件回调,当自定义组件内该自定义事件触发时,会触发卡片页面上注册的回调事件。 39 40> **说明:** 41> 42> 事件名不支持大写字母。 43 44**自定义子组件示例:** 45 46```html 47<!-- comp.hml --> 48<div class="container"> 49 <div class="row-3" if="true"> 50 <button onclick="buttonClicked" value="click"></button> 51 </div> 52</div> 53``` 54 55```css 56/* comp.css */ 57.container { 58 flex-direction:column; 59 background-color: green; 60 width: 100%; 61 height: 100%; 62} 63 64.row-3 { 65 width: 100%; 66 height: 50px; 67 background-color: orange; 68 font-size:15px; 69} 70``` 71 72```json 73{ 74 "data": { 75 }, 76 "actions": { 77 "buttonClicked": { 78 "action": "proxy", 79 "method":"event_1" 80 } 81 } 82} 83``` 84**父组件示例:** 85 86```html 87<!-- xxx.hml --> 88<element name='comp' src='../../common/customComponent/customComponent.hml'></element> 89 90<div class="container"> 91 <comp @event_1="click"></comp> 92 <button value="parentClick" @click="buttonClick"></button> 93</div> 94``` 95 96```css 97/* xxx.css */ 98.container { 99 background-color: red; 100 height: 500px; 101 width: 500px; 102} 103``` 104 105```json 106{ 107 "data": { 108 }, 109 "actions": { 110 "click": { 111 "action": "message", 112 "params": { 113 "message": "click event" 114 } 115 }, 116 "buttonClick": { 117 "action": "message", 118 "params": { 119 "message": "click event 2" 120 } 121 } 122 } 123} 124``` 125 126 127## props 128 129自定义组件可以通过props声明自定义属性,父组件通过设置属性向子组件传递参数。 130 131### 添加默认值 132 133子组件可以通过固定值default设置默认值,当父组件没有设置该属性时,将使用其默认值。此情况下props属性必须为对象形式,不能用数组形式,示例如下: 134 135```html 136<!-- comp.hml --> 137<div class="container"> 138 <div class="row-1"> 139 <div class="box-1"> 140 <text>xiaoziti</text> 141 </div> 142 <div class="box-2"> 143 <text>{{text}}</text> 144 </div> 145 <div class="box-3"> 146 <text>{{textdata[0]}}</text> 147 </div> 148 </div> 149 <div class="row-2" if="true"> 150 <button value="{{progress}}"></button> 151 </div> 152 <div class="row-3" if="true"> 153 <button onclick="buttonClicked" value="click"></button> 154 </div> 155</div> 156``` 157 158```json 159{ 160 "data": { 161 "progress": { 162 "default": "80" 163 } 164 }, 165 "props": { 166 "textdata": { 167 "default": ["a","b"] 168 }, 169 "progress": { 170 "default": 60 171 }, 172 "text": { 173 "default": "ha" 174 } 175 }, 176 "actions": { 177 "buttonClicked": { 178 "action": "proxy", 179 "method": "event_1" 180 } 181 } 182} 183``` 184 185引用子组件comp的父组件示例如下: 186 187```html 188<!-- xxx.hml --> 189<element name='comp' src='../../common/customComponent/customComponent.hml'></element> 190 191<div class="container"> 192 <comp progress="{{clicknow}}" textdata="{{texts}}" if="false" @event_1="click"></comp> 193</div> 194``` 195 196### 数据单向性 197 198父子组件之间数据的传递是单向的,只能从父组件传递给子组件,子组件不能直接修改父组件传递下来的值,可以将props传入的值用data接收后作为默认值,再对data的值进行修改。 199 200更多说明请参考[props](../arkui-js/js-components-custom-props.md)文档。