1# props 2 3You can use **props** to declare attributes of a custom component and pass the attributes to the child component. The supported types of **props** include String, Number, Boolean, Array, Object, and Function. Note that a camel case attribute name \(**compProp**\) should be converted to the kebab case format \(**comp-prop**\) when you reference the attribute in an external parent component. You can add **props** to a custom component, and pass the attribute to the child component. 4 5``` 6<!-- comp.hml --> 7<div class="item"> 8 <text class="title-style">{{compProp}}</text> 9</div> 10``` 11 12``` 13// comp.js 14export default { 15 props: ['compProp'], 16} 17``` 18 19``` 20<!-- xxx.hml --> 21<element name='comp' src='../../common/component/comp/comp.hml'></element> 22<div class="container"> 23 <comp comp-prop="{{title}}"></comp> 24</div> 25``` 26 27> **NOTE:** 28>The name of a custom attribute cannot start with reserved keywords such as **on**, **@**, **on:**, and **grab:**. 29 30## Default Value 31 32You 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. 33 34``` 35<!-- comp.hml --> 36<div class="item"> 37 <text class="title-style">{{title}}</text> 38</div> 39``` 40 41``` 42// comp.js 43export default { 44 props: { 45 title: { 46 default: 'title', 47 }, 48 }, 49} 50``` 51 52In this example, a **<text\>** component is added to display the title. The title content is a custom attribute, which displays the text specified by a user. If the user has not set a title, the default text will be displayed. Add the attribute when referencing the custom component. 53 54``` 55<!-- xxx.hml --> 56<element name='comp' src='../../common/component/comp/comp.hml'></element> 57<div class="container"> 58<comp title=" Custom component "></comp> 59</div> 60``` 61 62## Unidirectional Value Transfer 63 64Data 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. 65 66``` 67// comp.js 68export default { 69 props: ['defaultCount'], 70 data() { 71 return { 72 count: this.defaultCount, 73 }; 74 }, 75 onClick() { 76 this.count = this.count + 1; 77 }, 78} 79``` 80 81## Monitoring Data Changes by $watch 82 83To listen for attribute changes in a component, you can use the **$watch** method to add a callback. The following code snippet shows how to use **$watch**: 84 85``` 86// comp.js 87export default { 88 props: ['title'], 89 onInit() { 90 this.$watch('title', 'onPropertyChange'); 91 }, 92 onPropertyChange(newV, oldV) { 93 console.info('title attribute changed'+ newV + ' ' + oldV) 94 }, 95} 96``` 97 98## Computed Property 99 100To improve the development efficiency, you can use a computed property to pre-process an attribute in a custom component before reading or setting the attribute. In the **computed** field, you can set a getter or setter to be triggered when the attribute is read or written, respectively. The following is an example: 101 102``` 103// comp.js 104export default { 105 props: ['title'], 106 data() { 107 return { 108 objTitle: this.title, 109 time: 'Today', 110 }; 111 }, 112 computed: { 113 message() { 114 return this.time + ' ' + this.objTitle; 115 }, 116 notice: { 117 get() { 118 return this.time; 119 }, 120 set(newValue) { 121 this.time = newValue; 122 }, 123 }, 124 }, 125 onClick() { 126 console.info('get click event ' + this.message); 127 this.notice = 'Tomorrow'; 128 }, 129} 130``` 131 132The first computed property **message** has only a getter. The value of **message** changes depending on the **objTitle** value. The getter can only read but cannot set the value. You need a setter \(such as **notice** in the sample code\) to set the value of the computed property. 133 134