start-with-js.md 8.5 KB
Newer Older
G
ge-yafang 已提交
1 2
# Getting Started with JavaScript in the Traditional Coding Approach

E
ester.zhou 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>For best possible results, use [DevEco Studio V3.0.0.900 Beta3](https://developer.harmonyos.com/cn/develop/deveco-studio#download_beta_openharmony) for your development.
>
G
ge-yafang 已提交
5 6 7 8


## Creating a JavaScript Project

E
ester.zhou 已提交
9
1. In DevEco Studio, if no project is open, click **Create Project**; if a project is already open, choose **File** &gt; **New** &gt; **Create Project**. Then, select **Empty Ability** and click **Next**.
10

G
ge-yafang 已提交
11 12 13
   ![en-us_image_0000001223558814](figures/en-us_image_0000001223558814.png)

2. On the project configuration page, set **UI Syntax** to **JS** and retain the default values for other parameters.
14

G
ge-yafang 已提交
15 16 17 18 19 20 21
   ![en-us_image_0000001223877162](figures/en-us_image_0000001223877162.png)

3. Click **Finish**. DevEco Studio will automatically generate the sample code and resources that match your project type. Wait until the project is created.


## JavaScript Project Files

E
ester.zhou 已提交
22
- **entry**: OpenHarmony project module, which can be built into an ability package ([HAP](../../glossary.md#hap)).
E
ester.zhou 已提交
23 24 25 26 27 28 29 30 31 32 33
  - **src &gt; main &gt; js**: a collection of JS source code.
  - **src &gt; main &gt; js &gt; MainAbility**: entry to your application/service.
  - **src &gt; main &gt; js &gt; MainAbility &gt; i18n**: resources in different languages, for example, UI strings and image paths.
  - **src &gt; main &gt; js &gt; MainAbility &gt; pages**: pages contained in **MainAbility**.
  - **src &gt; main &gt; js &gt; MainAbility &gt; app.js**: ability lifecycle file.
  - **src &gt; main &gt; resources**: a collection of resource files used by your application/service, such as graphics, multimedia, character strings, and layout files.
  - **src &gt; main &gt; config.json**: module configuration file. This file describes the global configuration information of the application/service, the device-specific configuration information, and the configuration information of the HAP file.
  - **build-profile.json5**: current module information and build configuration options, including **buildOption target**.
  - **hvigorfile.js**: module-level compilation and build task script. You can customize related tasks and code implementation.
- **build-profile.json5**: application-level configuration information, including the signature and product configuration.
- **hvigorfile.js**: application-level compilation and build task script.
G
ge-yafang 已提交
34 35 36 37 38


## Building the First Page

1. Use the **Text** component.
E
ester.zhou 已提交
39
   
G
ge-yafang 已提交
40 41
   After the project synchronization is complete, choose **entry** &gt; **src** &gt; **main** &gt; **js** &gt; **MainAbility** &gt; **pages** &gt; **index** in the **Project** window and open the **index.hml** file. You can see that the file contains a **&lt;Text&gt;** component. The sample code in the **index.hml** file is shown below:

E
ester.zhou 已提交
42

43 44
   ```html
   <!--index.hml-->
G
ge-yafang 已提交
45 46 47 48 49 50 51 52
   <div class="container">
       <text class="title">
           Hello World
       </text>
   </div>
   ```

2. Add a button and bind the **onclick** method to this button.
E
ester.zhou 已提交
53

E
ester.zhou 已提交
54
   On the default page, add an **&lt;input&gt;** component of the button type to respond to user clicks and implement redirection to another page. The sample code in the **index.hml** file is shown below:
G
ge-yafang 已提交
55

E
ester.zhou 已提交
56

57 58
   ```html
   <!--index.hml-->
G
ge-yafang 已提交
59 60 61 62 63 64 65 66 67 68 69
   <div class="container">
       <text class="title">
           Hello World
       </text>
   
   <!-- Add a button, set its value to Next, and bind the onclick method to the button. -->
       <input class="btn" type="button" value="Next" onclick="onclick"></input>
   </div>
   ```

3. Set the page style in the **index.css** file.
E
ester.zhou 已提交
70

G
ge-yafang 已提交
71 72
   From the **Project** window, choose **entry** &gt; **src** &gt; **main** &gt; **js** &gt; **MainAbility** &gt; **pages** &gt; **index**, open the **index.css** file, and set the page styles, such as the width, height, font size, and spacing. The sample code in the **index.css** file is shown below:

E
ester.zhou 已提交
73

74
   ```css
G
ge-yafang 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
   .container {
       display: flex;
       flex-direction: column;
       justify-content: center;
       align-items: center;
       left: 0px;
       top: 0px;
       width: 100%;
       height: 100%;
   }
   
   .title {
       font-size: 100px;
       font-weight: bold;
       text-align: center;
       width: 100%;
       margin: 10px;
   }
   
   .btn {
       font-size: 60px;
       font-weight: bold;
       text-align: center;
       width: 40%;
       height: 5%;
       margin-top: 20px;
   }
   ```

E
ester.zhou 已提交
104 105 106 107
4. On the toolbar in the upper right corner of the editing window, click **Previewer** to open the Previewer. 

   Below is how the first page looks on the Previewer.

G
ge-yafang 已提交
108 109 110 111 112 113
   ![en-us_image_0000001216084724](figures/en-us_image_0000001216084724.png)


## Building the Second Page

1. Create the second page.
E
ester.zhou 已提交
114 115
   
   In the **Project** window, choose **entry** &gt; **src** &gt; **main** &gt; **js** &gt; **MainAbility**, right-click the **pages** folder, choose **New** &gt; **Page**, name the page **second**, and click **Finish**. Below, you can see the structure of the **second** folder.
G
ge-yafang 已提交
116

E
ester.zhou 已提交
117
![en-us_image_0000001223877210](figures/en-us_image_0000001223877210.png)
G
ge-yafang 已提交
118 119

2. Add **&lt;Text&gt;** and **&lt;Button&gt;** components.
E
ester.zhou 已提交
120

G
ge-yafang 已提交
121 122
   Add **&lt;Text&gt;** and **&lt;Button&gt;** components and set their styles, as you do for the first page. The sample code in the **second.hml** file is shown below:

E
ester.zhou 已提交
123

124 125
   ```html
   <!--second.hml-->
G
ge-yafang 已提交
126 127 128 129 130 131 132 133 134 135 136
   <div class="container">
       <text class="title">
           Hi there
       </text>
   
   <!-- Add a button, set the value to Back, and bind the back method to the button.-->
       <input class="btn" type="button" value="Back" onclick="back"></input>
   </div>
   ```

3. Set the page style in the **second.css** file. The sample code in the **second.css** file is shown below:
E
ester.zhou 已提交
137

138
   ```css
G
ge-yafang 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
   .container {
       display: flex;
       flex-direction: column;
       justify-content: center;
       align-items: center;
       left: 0px;
       top: 0px;
       width: 100%;
       height: 100%;
   }
   
   .title {
       font-size: 100px;
       font-weight: bold;
       text-align: center;
       width: 100%;
       margin: 10px;
   }
   
   .btn {
       font-size: 60px;
       font-weight: bold;
       text-align: center;
       width: 40%;
       height: 5%;
       margin-top: 20px;
   }
   ```


## Implementing Page Redirection

You can implement page redirection through the [page router](../ui/ui-js-building-ui-routes.md), which finds the target page based on the page URI. Import the **router** module and then perform the steps below:

1. Implement redirection from the first page to the second page.
E
ester.zhou 已提交
174
   
G
ge-yafang 已提交
175 176
   In the **index.js** file of the first page, bind the **onclick** method to the button so that clicking the button redirects the user to the second page. The sample code in the **index.js** file is shown below:

E
ester.zhou 已提交
177

178 179
   ```js
   // index.js
180
   import router from '@ohos.router';
G
ge-yafang 已提交
181 182 183 184
   
   export default {
       onclick: function () {
           router.push({
185
               url: "pages/second/second"
G
ge-yafang 已提交
186 187 188 189 190 191
           })
       }
   }
   ```

2. Implement redirection from the second page to the first page.
E
ester.zhou 已提交
192

G
ge-yafang 已提交
193 194
   In the **second.ets** file of the second page, bind the **back** method to the **Back** button so that clicking the button redirects the user back to the first page. The sample code in the **second.js** file is shown below:

E
ester.zhou 已提交
195

196 197
   ```js
   // second.js
198
   import router from '@ohos.router';
G
ge-yafang 已提交
199 200 201 202 203 204 205 206 207
   
   export default {
       back: function () {
           router.back()
       }
   }
   ```

3. Open any file in the **index** folder and click ![en-us_image_0000001262339067](figures/en-us_image_0000001262339067.png) in the Previewer to refresh the file. The figure below shows the effect.
208

G
ge-yafang 已提交
209 210 211 212 213 214 215 216
   ![en-us_image_0000001216269940](figures/en-us_image_0000001216269940.png)


## Running the Application on a Real Device

1. Connect the development board running the OpenHarmony standard system to the computer.

2. Choose **File** &gt; **Project Structure** &gt; **Project** &gt; **Signing Configs**, select **Automatically generate signing**, wait until the automatic signing is complete, and click **OK**, as shown below.
217

G
ge-yafang 已提交
218 219 220
   ![en-us_image_0000001223557290](figures/en-us_image_0000001223557290.png)

3. On the toolbar in the upper right corner of the editing window, click ![en-us_image_0000001217047316](figures/en-us_image_0000001217047316.png). The display effect is shown in the figure below.
221

G
ge-yafang 已提交
222 223 224
   ![en-us_image_0000001217527892](figures/en-us_image_0000001217527892.png)

Congratulations! You have finished developing your OpenHarmony application in JavaScript in the traditional coding approach. To learn more about OpenHarmony, see [OpenHarmony Overview](../application-dev-guide.md)