1e41f4b71Sopenharmony_ci# Basic Usage of Custom Components
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_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 import a custom component to the host page through **element** as shown in the following code snippet:
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci```html
6e41f4b71Sopenharmony_ci<element name='comp' src='../common/component/comp.hml'></element>
7e41f4b71Sopenharmony_ci<div>
8e41f4b71Sopenharmony_ci  <comp prop1='xxxx' @child1="bindParentVmMethod"></comp>
9e41f4b71Sopenharmony_ci</div>
10e41f4b71Sopenharmony_ci```
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ciThe following is an example of using a custom component with **if-else**, which displays **comp1** when **showComp1** is set to **true** and displays **comp2** otherwise.
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci```html
15e41f4b71Sopenharmony_ci<element name='comp1' src='../common/component/comp1/comp1.hml'></element>
16e41f4b71Sopenharmony_ci<element name='comp2' src='../common/component/comp2/comp2.hml'></element>
17e41f4b71Sopenharmony_ci<div>
18e41f4b71Sopenharmony_ci  <comp1 if="{{showComp1}}" prop1='xxxx' @child1="bindParentVmMethodOne"></comp1>
19e41f4b71Sopenharmony_ci  <comp2 else prop1='xxxx' @child1="bindParentVmMethodTwo"></comp2>
20e41f4b71Sopenharmony_ci</div>
21e41f4b71Sopenharmony_ci```
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ciThe **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.
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci## Custom Events
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ciTo bind an event to a custom child component, use the **(on|@)event-name="bindParentVmMethod"** syntax. **this.$emit('eventName', { params: 'passedParameters' })** is used in the child component to trigger the event and pass parameters to the parent component. The parent component then calls the **bindParentVmMethod** API and receives parameters passed by the child component.
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci>  **NOTE**
31e41f4b71Sopenharmony_ci>
32e41f4b71Sopenharmony_ci>  For child component events that are named in camel case, convert the names to kebab case when binding the events to the parent component. For example, use **\@children-event** in the parent component instead of **childrenEvent** used in the child component.
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci**Example 1 with parameters passed**
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ciThe following example describes how to define a child component **comp**:
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci```html
39e41f4b71Sopenharmony_ci<!-- comp.hml -->
40e41f4b71Sopenharmony_ci<div class="item">  
41e41f4b71Sopenharmony_ci   <text class="text-style" onclick="childClicked">Click here to view the hidden text.</text> 
42e41f4b71Sopenharmony_ci   <text class="text-style" if="{{showObj}}">hello world</text> 
43e41f4b71Sopenharmony_ci</div>
44e41f4b71Sopenharmony_ci```
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci```css
47e41f4b71Sopenharmony_ci/* comp.css */
48e41f4b71Sopenharmony_ci.item {  
49e41f4b71Sopenharmony_ci  width: 700px;   
50e41f4b71Sopenharmony_ci  flex-direction: column;   
51e41f4b71Sopenharmony_ci  height: 300px;   
52e41f4b71Sopenharmony_ci  align-items: center;   
53e41f4b71Sopenharmony_ci  margin-top: 100px;  
54e41f4b71Sopenharmony_ci} 
55e41f4b71Sopenharmony_ci.text-style { 
56e41f4b71Sopenharmony_ci  font-weight: 500; 
57e41f4b71Sopenharmony_ci  font-family: Courier; 
58e41f4b71Sopenharmony_ci  font-size: 40px;
59e41f4b71Sopenharmony_ci}
60e41f4b71Sopenharmony_ci```
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci```js
63e41f4b71Sopenharmony_ci// comp.js
64e41f4b71Sopenharmony_ciexport default { 
65e41f4b71Sopenharmony_ci  data: {  
66e41f4b71Sopenharmony_ci    showObj: false,  
67e41f4b71Sopenharmony_ci  },  
68e41f4b71Sopenharmony_ci  childClicked () {  
69e41f4b71Sopenharmony_ci    this.$emit('eventType1'); 
70e41f4b71Sopenharmony_ci    this.showObj = !this.showObj;  
71e41f4b71Sopenharmony_ci  },  
72e41f4b71Sopenharmony_ci}
73e41f4b71Sopenharmony_ci```
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ciThe following example describes how to import **comp** to the parent component:
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci```html
78e41f4b71Sopenharmony_ci<!-- xxx.hml --> 
79e41f4b71Sopenharmony_ci<element name='comp' src='../common/component/comp.hml'></element>  
80e41f4b71Sopenharmony_ci<div class="container">  
81e41f4b71Sopenharmony_ci  <comp @event-type1="textClicked"></comp>  
82e41f4b71Sopenharmony_ci</div>
83e41f4b71Sopenharmony_ci```
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci```css
86e41f4b71Sopenharmony_ci/* xxx.css */
87e41f4b71Sopenharmony_ci.container {  
88e41f4b71Sopenharmony_ci  background-color: #f8f8ff;  
89e41f4b71Sopenharmony_ci  flex: 1;  
90e41f4b71Sopenharmony_ci  flex-direction: column;  
91e41f4b71Sopenharmony_ci  align-content: center; 
92e41f4b71Sopenharmony_ci} 
93e41f4b71Sopenharmony_ci```
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci```js
96e41f4b71Sopenharmony_ci// xxx.js
97e41f4b71Sopenharmony_ciexport default {    
98e41f4b71Sopenharmony_ci  textClicked () {} 
99e41f4b71Sopenharmony_ci}
100e41f4b71Sopenharmony_ci```
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci**Example 2 with no parameters passed**
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ciThe following example describes how to define a child component **comp**:
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ci```html
107e41f4b71Sopenharmony_ci<!-- comp.hml -->
108e41f4b71Sopenharmony_ci<div class="item">  
109e41f4b71Sopenharmony_ci   <text class="text-style" onclick="childClicked">Click here to view the hidden text.</text> 
110e41f4b71Sopenharmony_ci   <text class="text-style" if="{{ showObj }}">hello world</text> 
111e41f4b71Sopenharmony_ci</div>
112e41f4b71Sopenharmony_ci```
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ci```js
115e41f4b71Sopenharmony_ci// comp.js
116e41f4b71Sopenharmony_ciexport default { 
117e41f4b71Sopenharmony_ci  childClicked () {
118e41f4b71Sopenharmony_ci    this.$emit('eventType1', { text: 'Receive the parameters from the child component.' });
119e41f4b71Sopenharmony_ci    this.showObj = !this.showObj;
120e41f4b71Sopenharmony_ci  },
121e41f4b71Sopenharmony_ci}
122e41f4b71Sopenharmony_ci```
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ciIn the following example, the child component passes the **text** parameter to the parent component, and the parent component obtains the parameter through **e.detail**:
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci```html
127e41f4b71Sopenharmony_ci<!-- xxx.hml -->
128e41f4b71Sopenharmony_ci<element name='comp' src='../common/comp/comp.hml'></element>
129e41f4b71Sopenharmony_ci<div class="container">  
130e41f4b71Sopenharmony_ci   <text>Parent component: {{text}}</text> 
131e41f4b71Sopenharmony_ci   <comp @event-type1="textClicked"></comp>  
132e41f4b71Sopenharmony_ci</div>
133e41f4b71Sopenharmony_ci```
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci```js
136e41f4b71Sopenharmony_ci// xxx.js
137e41f4b71Sopenharmony_ciexport default { 
138e41f4b71Sopenharmony_ci  data: {
139e41f4b71Sopenharmony_ci    text: 'Start'
140e41f4b71Sopenharmony_ci  },
141e41f4b71Sopenharmony_ci  textClicked (e) {
142e41f4b71Sopenharmony_ci    this.text = e.detail.text;
143e41f4b71Sopenharmony_ci  },
144e41f4b71Sopenharmony_ci}
145e41f4b71Sopenharmony_ci```
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci![EventParameters](figures/EventParameters.gif)
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci## Custom Component Data
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ciIn the JS file of a custom component, you can define, pass, and process data by declaring fields such as **data**, **props**, and **computed**. For details about how to use **props** and **computed**, see [Data Transfer and Processing](js-components-custom-props.md).
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci**Table 1** Custom component data
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ci| Name     | Type           | Description                                    |
158e41f4b71Sopenharmony_ci| -------- | --------------- | ---------------------------------------- |
159e41f4b71Sopenharmony_ci| data     | Object \| Function | Data model of the page. If the attribute is of the function type, the return value must be of the object type. The name cannot start with a dollar sign ($) or underscore (\_). Do not use reserved words (**for**, **if**, **show**, and **tid**).<br>Do not use this attribute and **private** or **public** at the same time.|
160e41f4b71Sopenharmony_ci| props    | Array \| Object    | Used for communication between components. This attribute can be passed to components through **\<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**) in the name. Currently, **props** does not support functions.|
161e41f4b71Sopenharmony_ci| computed | Object          | Used for pre-processing for reading and setting parameters. The result is cached. The name cannot start with a dollar sign ($) or underscore (\_). Do not use reserved words.|
162