1e41f4b71Sopenharmony_ci# Custom Component Basic Usage 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci> **NOTE** 5e41f4b71Sopenharmony_ci> 6e41f4b71Sopenharmony_ci> The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciCustom components are existing components encapsulated based on service requirements. A custom component can be invoked multiple times in a project to improve the code readability. You can introduce a custom component to the host page through **element** as shown in the following code snippet: 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```html 12e41f4b71Sopenharmony_ci<element name='comp' src='../../common/component/comp.hml'></element> 13e41f4b71Sopenharmony_ci<div> 14e41f4b71Sopenharmony_ci <comp prop1='xxxx' @child1="bindParentVmMethod"></comp> 15e41f4b71Sopenharmony_ci</div> 16e41f4b71Sopenharmony_ci``` 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci- The **name** attribute indicates the custom component name (optional), which is case-insensitive and is in lowercase by default. The **src** attribute indicates the **.hml** file path (mandatory) of the custom component. If **name** is not set, the **.hml** file name is used as the component name by default. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci- Event binding: Use **(on|\@)*child1*** in the custom component to bind a child component event. In the child component, use **{action:"*proxy*", method: "*eventName*"}** to trigger the bound event and pass the parameter value. In the parent component, call **bindParentVmMethod** and receive the parameter passed from the child component. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci## Labels of Custom Component Configuration File 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci| Name| Type| Description| 25e41f4b71Sopenharmony_ci| -------- | -------- | -------- | 26e41f4b71Sopenharmony_ci| data | Object | Data model of the page. The name cannot start with a dollar sign ($) or underscore (_). Do not use reserved words (**for**, **if**, **show**, and **tid**).| 27e41f4b71Sopenharmony_ci| props | Array/Object | Used for communication between components. This attribute can be transferred to components via **\<tag xxxx='value'>**. A **props** name must be in lowercase and cannot start with a dollar sign ($) or underscore (_). Do not use reserved words (**for**, **if**, **show**, and **tid**). Currently, **props** does not support functions. | 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci## Adding a Custom Event 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ciCustom components support custom events, whose **action** is **proxy** and name is specified by **method**. A service widget page that uses a custom component can register an event callback corresponding to the custom event name. When the custom event in the custom component is triggered, the registered callback will be invoked. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci> **NOTE** 35e41f4b71Sopenharmony_ci> 36e41f4b71Sopenharmony_ci> The event name cannot contain uppercase letters. 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ciExample of a custom child component: 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci```html 41e41f4b71Sopenharmony_ci<!-- comp.hml --> 42e41f4b71Sopenharmony_ci<div class="container"> 43e41f4b71Sopenharmony_ci <div class="row-3" if="true"> 44e41f4b71Sopenharmony_ci <button onclick="buttonClicked" value="click"></button> 45e41f4b71Sopenharmony_ci </div> 46e41f4b71Sopenharmony_ci</div> 47e41f4b71Sopenharmony_ci``` 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci```css 50e41f4b71Sopenharmony_ci/* comp.css */ 51e41f4b71Sopenharmony_ci.container { 52e41f4b71Sopenharmony_ci flex-direction:column; 53e41f4b71Sopenharmony_ci background-color: green; 54e41f4b71Sopenharmony_ci width: 100%; 55e41f4b71Sopenharmony_ci height: 100%; 56e41f4b71Sopenharmony_ci} 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci.row-3 { 59e41f4b71Sopenharmony_ci width: 100%; 60e41f4b71Sopenharmony_ci height: 50px; 61e41f4b71Sopenharmony_ci background-color: orange; 62e41f4b71Sopenharmony_ci font-size:15px; 63e41f4b71Sopenharmony_ci} 64e41f4b71Sopenharmony_ci``` 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci```json 67e41f4b71Sopenharmony_ci{ 68e41f4b71Sopenharmony_ci "data": { 69e41f4b71Sopenharmony_ci }, 70e41f4b71Sopenharmony_ci "actions": { 71e41f4b71Sopenharmony_ci "buttonClicked": { 72e41f4b71Sopenharmony_ci "action": "proxy", 73e41f4b71Sopenharmony_ci "method":"event_1" 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci } 76e41f4b71Sopenharmony_ci} 77e41f4b71Sopenharmony_ci``` 78e41f4b71Sopenharmony_ciExample of a parent component: 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci```html 81e41f4b71Sopenharmony_ci<!-- xxx.hml --> 82e41f4b71Sopenharmony_ci<element name='comp' src='../../common/customComponent/customComponent.hml'></element> 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci<div class="container"> 85e41f4b71Sopenharmony_ci <comp @event_1="click"></comp> 86e41f4b71Sopenharmony_ci <button value="parentClick" @click="buttonClick"></button> 87e41f4b71Sopenharmony_ci</div> 88e41f4b71Sopenharmony_ci``` 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci```css 91e41f4b71Sopenharmony_ci/* xxx.css */ 92e41f4b71Sopenharmony_ci.container { 93e41f4b71Sopenharmony_ci background-color: red; 94e41f4b71Sopenharmony_ci height: 500px; 95e41f4b71Sopenharmony_ci width: 500px; 96e41f4b71Sopenharmony_ci} 97e41f4b71Sopenharmony_ci``` 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci```json 100e41f4b71Sopenharmony_ci{ 101e41f4b71Sopenharmony_ci "data": { 102e41f4b71Sopenharmony_ci }, 103e41f4b71Sopenharmony_ci "actions": { 104e41f4b71Sopenharmony_ci "click": { 105e41f4b71Sopenharmony_ci "action": "message", 106e41f4b71Sopenharmony_ci "params": { 107e41f4b71Sopenharmony_ci "message": "click event" 108e41f4b71Sopenharmony_ci } 109e41f4b71Sopenharmony_ci }, 110e41f4b71Sopenharmony_ci "buttonClick": { 111e41f4b71Sopenharmony_ci "action": "message", 112e41f4b71Sopenharmony_ci "params": { 113e41f4b71Sopenharmony_ci "message": "click event 2" 114e41f4b71Sopenharmony_ci } 115e41f4b71Sopenharmony_ci } 116e41f4b71Sopenharmony_ci } 117e41f4b71Sopenharmony_ci} 118e41f4b71Sopenharmony_ci``` 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci## props 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ciYou can use **props** to declare custom attributes of a custom component. The attributes can be used by a parent component to pass parameters to a child component. 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci### Default Value 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ciYou can set the default value for a child component attribute via **default**. The default value is used if the parent component does not have **default** set. In this case, the **props** attribute must be in object format instead of an array. 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci```html 130e41f4b71Sopenharmony_ci<!-- comp.hml --> 131e41f4b71Sopenharmony_ci<div class="container"> 132e41f4b71Sopenharmony_ci <div class="row-1"> 133e41f4b71Sopenharmony_ci <div class="box-1"> 134e41f4b71Sopenharmony_ci <text>xiaoziti</text> 135e41f4b71Sopenharmony_ci </div> 136e41f4b71Sopenharmony_ci <div class="box-2"> 137e41f4b71Sopenharmony_ci <text>{{text}}</text> 138e41f4b71Sopenharmony_ci </div> 139e41f4b71Sopenharmony_ci <div class="box-3"> 140e41f4b71Sopenharmony_ci <text>{{textdata[0]}}</text> 141e41f4b71Sopenharmony_ci </div> 142e41f4b71Sopenharmony_ci </div> 143e41f4b71Sopenharmony_ci <div class="row-2" if="true"> 144e41f4b71Sopenharmony_ci <button value="{{progress}}"></button> 145e41f4b71Sopenharmony_ci </div> 146e41f4b71Sopenharmony_ci <div class="row-3" if="true"> 147e41f4b71Sopenharmony_ci <button onclick="buttonClicked" value="click"></button> 148e41f4b71Sopenharmony_ci </div> 149e41f4b71Sopenharmony_ci</div> 150e41f4b71Sopenharmony_ci``` 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci```json 153e41f4b71Sopenharmony_ci{ 154e41f4b71Sopenharmony_ci "data": { 155e41f4b71Sopenharmony_ci "progress": { 156e41f4b71Sopenharmony_ci "default": "80" 157e41f4b71Sopenharmony_ci } 158e41f4b71Sopenharmony_ci }, 159e41f4b71Sopenharmony_ci "props": { 160e41f4b71Sopenharmony_ci "textdata": { 161e41f4b71Sopenharmony_ci "default": ["a","b"] 162e41f4b71Sopenharmony_ci }, 163e41f4b71Sopenharmony_ci "progress": { 164e41f4b71Sopenharmony_ci "default": 60 165e41f4b71Sopenharmony_ci }, 166e41f4b71Sopenharmony_ci "text": { 167e41f4b71Sopenharmony_ci "default": "ha" 168e41f4b71Sopenharmony_ci } 169e41f4b71Sopenharmony_ci }, 170e41f4b71Sopenharmony_ci "actions": { 171e41f4b71Sopenharmony_ci "buttonClicked": { 172e41f4b71Sopenharmony_ci "action": "proxy", 173e41f4b71Sopenharmony_ci "method": "event_1" 174e41f4b71Sopenharmony_ci } 175e41f4b71Sopenharmony_ci } 176e41f4b71Sopenharmony_ci} 177e41f4b71Sopenharmony_ci``` 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ciExample of the parent component that references the **comp** child component: 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci```html 182e41f4b71Sopenharmony_ci<!-- xxx.hml --> 183e41f4b71Sopenharmony_ci<element name='comp' src='../../common/customComponent/customComponent.hml'></element> 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci<div class="container"> 186e41f4b71Sopenharmony_ci <comp progress="{{clicknow}}" textdata="{{texts}}" if="false" @event_1="click"></comp> 187e41f4b71Sopenharmony_ci</div> 188e41f4b71Sopenharmony_ci``` 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci### Unidirectional Value Transfer 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ciData can be transferred only from the parent component to child components. You are not allowed to change the value passed to the child component. However, you can receive the value passed by **props** as a default value in **data**, and then change the **data** value. 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ciFor details, see [props](../arkui-js/js-components-custom-props.md). 195