1e41f4b71Sopenharmony_ci# JavaScript 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciYou can use a .js file in the ECMAScript compliant JavaScript language to define the service logic of an HML page. With dynamic typing, JavaScript can make your application more expressive with a flexible design. The following describes the JavaScript compilation and running. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Syntax 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciThe ES6 syntax is supported. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci- Module declaration 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci Import functionality modules. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci ```js 16e41f4b71Sopenharmony_ci import router from '@ohos.router'; 17e41f4b71Sopenharmony_ci ``` 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci- Code reference 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci Import JavaScript code. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci ```js 24e41f4b71Sopenharmony_ci import utils from '../../common/utils.js'; 25e41f4b71Sopenharmony_ci ``` 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci## Objects 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci- Application objects 31e41f4b71Sopenharmony_ci | Name | Type | Description | 32e41f4b71Sopenharmony_ci | -------- | -------- | -------- | 33e41f4b71Sopenharmony_ci | $def | Object | Object that is exposed in the **app.js** file and obtained by **this.$app.$def**.<br>**NOTE**<br>Application objects do not support data binding. Data update should be triggered on the UI. | 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci Example 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci ```js 38e41f4b71Sopenharmony_ci // app.js 39e41f4b71Sopenharmony_ci export default { 40e41f4b71Sopenharmony_ci onCreate() { 41e41f4b71Sopenharmony_ci console.info('Application onCreate'); 42e41f4b71Sopenharmony_ci }, 43e41f4b71Sopenharmony_ci onDestroy() { 44e41f4b71Sopenharmony_ci console.info('Application onDestroy'); 45e41f4b71Sopenharmony_ci }, 46e41f4b71Sopenharmony_ci globalData: { 47e41f4b71Sopenharmony_ci appData: 'appData', 48e41f4b71Sopenharmony_ci appVersion: '2.0', 49e41f4b71Sopenharmony_ci }, 50e41f4b71Sopenharmony_ci globalMethod() { 51e41f4b71Sopenharmony_ci console.info('This is a global method!'); 52e41f4b71Sopenharmony_ci this.globalData.appVersion = '3.0'; 53e41f4b71Sopenharmony_ci } 54e41f4b71Sopenharmony_ci }; 55e41f4b71Sopenharmony_ci ``` 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci ```js 59e41f4b71Sopenharmony_ci // index.js 60e41f4b71Sopenharmony_ci export default { 61e41f4b71Sopenharmony_ci data: { 62e41f4b71Sopenharmony_ci appData: 'localData', 63e41f4b71Sopenharmony_ci appVersion:'1.0', 64e41f4b71Sopenharmony_ci }, 65e41f4b71Sopenharmony_ci onInit() { 66e41f4b71Sopenharmony_ci this.appData = this.$app.$def.globalData.appData; 67e41f4b71Sopenharmony_ci this.appVersion = this.$app.$def.globalData.appVersion; 68e41f4b71Sopenharmony_ci }, 69e41f4b71Sopenharmony_ci invokeGlobalMethod() { 70e41f4b71Sopenharmony_ci this.$app.$def.globalMethod(); 71e41f4b71Sopenharmony_ci }, 72e41f4b71Sopenharmony_ci getAppVersion() { 73e41f4b71Sopenharmony_ci this.appVersion = this.$app.$def.globalData.appVersion; 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci } 76e41f4b71Sopenharmony_ci ``` 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci- Page objects 79e41f4b71Sopenharmony_ci | Name | Type | Description | 80e41f4b71Sopenharmony_ci | -------- | -------- | -------- | 81e41f4b71Sopenharmony_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 attribute 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. | 82e41f4b71Sopenharmony_ci | $refs | Object | DOM elements or child component instances that have registered the ref attribute. For example code, see [Obtaining a DOM element](#obtaining-a-dom-element). | 83e41f4b71Sopenharmony_ci | private | Object | Data model of the page. Private data attribute can be modified only on the current page. | 84e41f4b71Sopenharmony_ci | public | Object | Data model of the page. Behaviors of public data attributes are the same as those of the data attribute. | 85e41f4b71Sopenharmony_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. For details, see [props](../reference/apis-arkui/arkui-js/js-components-custom-props.md#props). | 86e41f4b71Sopenharmony_ci | computed | Object | Used for pre-processing an object for reading and setting. The result is cached. The name cannot start with a dollar sign ($) or underscore (_). Do not use reserved words. For details, see [computed](../reference/apis-arkui/arkui-js/js-components-custom-props.md#computed). | 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci## Methods 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci- Data methods 91e41f4b71Sopenharmony_ci | Name | Parameter | Description | 92e41f4b71Sopenharmony_ci | -------- | -------- | -------- | 93e41f4b71Sopenharmony_ci | $set | key: string, value: any | Adds an attribute or modifies an existing attribute.<br/>Usage:<br/>this.$set('_key_',_value_): Add an attribute. | 94e41f4b71Sopenharmony_ci | $delete | key: string | Deletes an attribute.<br/>Usage:<br/>this.$delete('key'): Delete an attribute. | 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci Example 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci ```js 100e41f4b71Sopenharmony_ci // index.js 101e41f4b71Sopenharmony_ci export default { 102e41f4b71Sopenharmony_ci data: { 103e41f4b71Sopenharmony_ci keyMap: { 104e41f4b71Sopenharmony_ci OS: 'OS', 105e41f4b71Sopenharmony_ci Version: '2.0', 106e41f4b71Sopenharmony_ci }, 107e41f4b71Sopenharmony_ci }, 108e41f4b71Sopenharmony_ci getAppVersion() { 109e41f4b71Sopenharmony_ci this.$set('keyMap.Version', '3.0'); 110e41f4b71Sopenharmony_ci console.info("keyMap.Version = " + this.keyMap.Version); // keyMap.Version = 3.0 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci this.$delete('keyMap'); 113e41f4b71Sopenharmony_ci console.info("keyMap.Version = " + this.keyMap); // log print: keyMap.Version = undefined 114e41f4b71Sopenharmony_ci } 115e41f4b71Sopenharmony_ci } 116e41f4b71Sopenharmony_ci ``` 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci- Public methods 119e41f4b71Sopenharmony_ci | Name | Parameter | Description | 120e41f4b71Sopenharmony_ci | -------- | -------- | -------- | 121e41f4b71Sopenharmony_ci | $element | id: string | Obtains the component with a specified ID. If no ID is specified, the root component is returned. For example code, see [Obtaining a DOM element](#obtaining-a-dom-element).<br/>Usage:<br/><div id='xxx'></div><br/>- `this.$element('xxx')`<br/>- this.$element('xxx'): Obtain the component whose ID is xxx.<br/>- this.$element(): Obtain the root component. | 122e41f4b71Sopenharmony_ci | $rootElement | N/A | Obtains the root element.<br/>Usage: this.\$rootElement().scrollTo({ duration: 500, position: 300 }), which scrolls the page by 300 px within 500 ms. | 123e41f4b71Sopenharmony_ci | $root | N/A | Obtains the root ViewModel instance. For example code, see [Obtaining the ViewModel](#obtaining-the-viewmodel). | 124e41f4b71Sopenharmony_ci | $parent | N/A | Obtains the parent ViewModel instance. For example code, see [Obtaining the ViewModel](#obtaining-the-viewmodel). | 125e41f4b71Sopenharmony_ci | $child | id: string | Obtains the ViewModel instance of a custom child component with a specified ID. For example code, see [Obtaining the ViewModel](#obtaining-the-viewmodel).<br/>Usage:<br/>this.\$child('xxx'): Obtain the ViewModel instance of a custom child component whose ID is _xxx_. | 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci- Event methods 128e41f4b71Sopenharmony_ci | Name | Parameter | Description | 129e41f4b71Sopenharmony_ci | -------- | -------- | -------- | 130e41f4b71Sopenharmony_ci | $watch | data: string, callback: string \| Function | Listens for attribute changes. If the value of the data attribute changes, the bound event is triggered. For details, see [Monitoring Data Changes by $watch](../reference/apis-arkui/arkui-js/js-components-custom-props.md#monitoring-data-changes-by-watch).<br/>Usage:<br/>this.$watch('key', callback) | 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci- Page methods 133e41f4b71Sopenharmony_ci | Name | Parameter | Description | 134e41f4b71Sopenharmony_ci | -------- | -------- | -------- | 135e41f4b71Sopenharmony_ci | scrollTo<sup>6+</sup> | scrollPageParam: ScrollPageParam | Scrolls the page to the target position. You can specify the position using the ID selector or scrolling distance. | 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci **Table 1** ScrollPageParam<sup>6+</sup> 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci | Name | Type | Default Value | Description | 140e41f4b71Sopenharmony_ci | -------- | -------- | -------- | -------- | 141e41f4b71Sopenharmony_ci | position | number | - | Position to scroll to. | 142e41f4b71Sopenharmony_ci | id | string | - | ID of the element to be scrolled to. | 143e41f4b71Sopenharmony_ci | duration | number | 300 | Scrolling duration, in milliseconds. | 144e41f4b71Sopenharmony_ci | timingFunction | string | ease | Animation curve for scrolling. Available options:<br/>[Animation Styles](../reference/apis-arkui/arkui-js/js-components-common-animation.md) | 145e41f4b71Sopenharmony_ci | complete | () => void | - | Callback to be invoked when the scrolling is complete. | 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci Example 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci ```js 151e41f4b71Sopenharmony_ci this.$rootElement().scrollTo({position: 0}) 152e41f4b71Sopenharmony_ci this.$rootElement().scrollTo({id: 'id', duration: 200, timingFunction: 'ease-in', complete: ()=>void}) 153e41f4b71Sopenharmony_ci ``` 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci## Obtaining a DOM Element 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci1. Use **$refs** to obtain a DOM element. 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci ```html 161e41f4b71Sopenharmony_ci <!-- index.hml --> 162e41f4b71Sopenharmony_ci <div class="container"> 163e41f4b71Sopenharmony_ci <image-animator class="image-player" ref="animator" images="{{images}}" duration="1s" onclick="handleClick"></image-animator> 164e41f4b71Sopenharmony_ci </div> 165e41f4b71Sopenharmony_ci ``` 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci ```js 168e41f4b71Sopenharmony_ci // index.js 169e41f4b71Sopenharmony_ci export default { 170e41f4b71Sopenharmony_ci data: { 171e41f4b71Sopenharmony_ci images: [ 172e41f4b71Sopenharmony_ci { src: '/common/frame1.png' }, 173e41f4b71Sopenharmony_ci { src: '/common/frame2.png' }, 174e41f4b71Sopenharmony_ci { src: '/common/frame3.png' } 175e41f4b71Sopenharmony_ci ] 176e41f4b71Sopenharmony_ci }, 177e41f4b71Sopenharmony_ci handleClick() { 178e41f4b71Sopenharmony_ci const animator = this.$refs.animator; // Obtain the DOM element whose $refs attribute is animator. 179e41f4b71Sopenharmony_ci const state = animator.getState(); 180e41f4b71Sopenharmony_ci if (state === 'paused') { 181e41f4b71Sopenharmony_ci animator.resume(); 182e41f4b71Sopenharmony_ci } else if (state === 'stopped') { 183e41f4b71Sopenharmony_ci animator.start(); 184e41f4b71Sopenharmony_ci } else { 185e41f4b71Sopenharmony_ci animator.pause(); 186e41f4b71Sopenharmony_ci } 187e41f4b71Sopenharmony_ci }, 188e41f4b71Sopenharmony_ci }; 189e41f4b71Sopenharmony_ci ``` 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci2. Call **$element** to obtain a DOM element. 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci ```html 194e41f4b71Sopenharmony_ci <!-- index.hml --> 195e41f4b71Sopenharmony_ci <div class="container" style="width:500px;height: 700px; margin: 100px;"> 196e41f4b71Sopenharmony_ci <image-animator class="image-player" id="animator" images="{{images}}" duration="1s" onclick="handleClick"></image-animator> 197e41f4b71Sopenharmony_ci </div> 198e41f4b71Sopenharmony_ci ``` 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci ```js 201e41f4b71Sopenharmony_ci // index.js 202e41f4b71Sopenharmony_ci export default { 203e41f4b71Sopenharmony_ci data: { 204e41f4b71Sopenharmony_ci images: [ 205e41f4b71Sopenharmony_ci { src: '/common/frame1.png' }, 206e41f4b71Sopenharmony_ci { src: '/common/frame2.png' }, 207e41f4b71Sopenharmony_ci { src: '/common/frame3.png' } 208e41f4b71Sopenharmony_ci ] 209e41f4b71Sopenharmony_ci }, 210e41f4b71Sopenharmony_ci handleClick() { 211e41f4b71Sopenharmony_ci const animator = this.$element('animator'); // Obtain the DOM element whose ID is animator. 212e41f4b71Sopenharmony_ci const state = animator.getState(); 213e41f4b71Sopenharmony_ci if (state === 'paused') { 214e41f4b71Sopenharmony_ci animator.resume(); 215e41f4b71Sopenharmony_ci } else if (state === 'stopped') { 216e41f4b71Sopenharmony_ci animator.start(); 217e41f4b71Sopenharmony_ci } else { 218e41f4b71Sopenharmony_ci animator.pause(); 219e41f4b71Sopenharmony_ci } 220e41f4b71Sopenharmony_ci }, 221e41f4b71Sopenharmony_ci }; 222e41f4b71Sopenharmony_ci ``` 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci## Obtaining the ViewModel 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ciThe following shows files of the root page: 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci```html 231e41f4b71Sopenharmony_ci<!-- root.hml --> 232e41f4b71Sopenharmony_ci<element name='parentComp' src='../../common/component/parent/parent.hml'></element> 233e41f4b71Sopenharmony_ci<div class="container"> 234e41f4b71Sopenharmony_ci <div class="container"> 235e41f4b71Sopenharmony_ci <text>{{text}}</text> 236e41f4b71Sopenharmony_ci <parentComp></parentComp> 237e41f4b71Sopenharmony_ci </div> 238e41f4b71Sopenharmony_ci</div> 239e41f4b71Sopenharmony_ci``` 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci```js 242e41f4b71Sopenharmony_ci// root.js 243e41f4b71Sopenharmony_ciexport default { 244e41f4b71Sopenharmony_ci data: { 245e41f4b71Sopenharmony_ci text: 'I am a root!', 246e41f4b71Sopenharmony_ci }, 247e41f4b71Sopenharmony_ci} 248e41f4b71Sopenharmony_ci``` 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ciCustomize the parent component. 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci```html 255e41f4b71Sopenharmony_ci<!-- parent.hml --> 256e41f4b71Sopenharmony_ci<element name='childComp' src='../child/child.hml'></element> 257e41f4b71Sopenharmony_ci<div class="item" onclick="textClicked"> 258e41f4b71Sopenharmony_ci <text class="text-style" onclick="parentClicked">parent component click</text> 259e41f4b71Sopenharmony_ci <text class="text-style" if="{{showValue}}">hello parent component!</text> 260e41f4b71Sopenharmony_ci <childComp id = "selfDefineChild"></childComp> 261e41f4b71Sopenharmony_ci</div> 262e41f4b71Sopenharmony_ci``` 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci```js 265e41f4b71Sopenharmony_ci// parent.js 266e41f4b71Sopenharmony_ciexport default { 267e41f4b71Sopenharmony_ci data: { 268e41f4b71Sopenharmony_ci showValue: false, 269e41f4b71Sopenharmony_ci text: 'I am parent component!', 270e41f4b71Sopenharmony_ci }, 271e41f4b71Sopenharmony_ci parentClicked () { 272e41f4b71Sopenharmony_ci this.showValue = !this.showValue; 273e41f4b71Sopenharmony_ci console.info('parent component get parent text'); 274e41f4b71Sopenharmony_ci console.info(`${this.$parent().text}`); 275e41f4b71Sopenharmony_ci console.info("The parent component gets the child function."); 276e41f4b71Sopenharmony_ci console.info(`${this.$child('selfDefineChild').childClicked()}`); 277e41f4b71Sopenharmony_ci }, 278e41f4b71Sopenharmony_ci} 279e41f4b71Sopenharmony_ci``` 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ciCustomize the child component. 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci```html 284e41f4b71Sopenharmony_ci<!-- child.hml --> 285e41f4b71Sopenharmony_ci<div class="item" onclick="textClicked"> 286e41f4b71Sopenharmony_ci <text class="text-style" onclick="childClicked">Child component clicked</text> 287e41f4b71Sopenharmony_ci <text class="text-style" if="{{isShow}}">Hello child component</text> 288e41f4b71Sopenharmony_ci</div> 289e41f4b71Sopenharmony_ci``` 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ci```js 292e41f4b71Sopenharmony_ci// child.js 293e41f4b71Sopenharmony_ciexport default { 294e41f4b71Sopenharmony_ci data: { 295e41f4b71Sopenharmony_ci isShow: false, 296e41f4b71Sopenharmony_ci text: 'I am the child component!', 297e41f4b71Sopenharmony_ci }, 298e41f4b71Sopenharmony_ci childClicked () { 299e41f4b71Sopenharmony_ci this.isShow = !this.isShow; 300e41f4b71Sopenharmony_ci console.info('child component get parent text'); 301e41f4b71Sopenharmony_ci console.info('${this.$parent().text}'); 302e41f4b71Sopenharmony_ci console.info('child component get root text'); 303e41f4b71Sopenharmony_ci console.info('${this.$root().text}'); 304e41f4b71Sopenharmony_ci }, 305e41f4b71Sopenharmony_ci} 306e41f4b71Sopenharmony_ci``` 307e41f4b71Sopenharmony_ci 308e41f4b71Sopenharmony_ci