• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Getting Started with JavaScript in FA Model
2
3
4> **NOTE**
5>
6> For best possible results, use [DevEco Studio V3.0.0.993](https://developer.harmonyos.com/cn/develop/deveco-studio#download) for your development.
7
8
9## Creating a JavaScript Project
10
111. If you are opening DevEco Studio for the first time, click **Create Project**. If a project is already open, choose **File** > **New** > **Create Project** from the menu bar. On the **OpenHarmony** tab of the **Choose Your Ability Template** page, select **Empty Ability** and click **Next**.
12
13   ![01](figures/01.png)
14
152. In the project configuration page, set **Compile SDK** to **8** or **9** (in the latter case, you also need to set **Model** to **FA**) and **Language** to **JS** and retain the default values for other parameters.
16
17   ![04](figures/04.png)
18
19   > **NOTE**
20   >
21   > If you are using DevEco Studio V2.2 Beta1 or later, you can use the low-code development mode apart from the traditional coding approach.
22   >
23   > On the low-code development pages, you can design your application UI in an efficient, intuitive manner, with a wide array of UI editing features.
24   >
25   > To use the low-code development mode, turn on **Enable Super Visual** on the page shown above.
26
273. Click **Finish**. DevEco Studio will automatically generate the sample code and resources that match your project type. Wait until the project is created.
28
29
30## JavaScript Project Directory Structure
31
32![en-us_image_0000001435376433](figures/en-us_image_0000001435376433.png)
33
34- **entry**: OpenHarmony project module, which can be built into an OpenHarmony Ability Package ([HAP](../../glossary.md#hap)).
35  - **src > main > js**: a collection of JavaScript source code.
36  - **src > main > js > MainAbility**: entry to your application/service.
37  - **src > main > js > MainAbility > i18n**: resources in different languages, for example, UI strings and image paths.
38  - **src > main > js > MainAbility > pages**: pages contained in **MainAbility**.
39  - **src > main > js > MainAbility > app.js**: ability lifecycle file.
40
41  - **src > main > resources**: a collection of resource files used by your application/service, such as graphics, multimedia, character strings, and layout files. For details about resource files, see [Resource Limitations and Access](../ui/js-framework-resource-restriction.md).
42  - **src > main > 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. For details, see [Application Configuration File Overview (FA Model)](application-configuration-file-overview-fa.md).
43  - **build-profile.json5**: current module information and build configuration options, including **buildOption** and **targets**.
44  - **hvigorfile.ts**: module-level build script. You can customize related tasks and code implementation.
45
46- **build-profile.json5**: application-level configuration information, including the signature and product configuration.
47
48- **hvigorfile.ts**: application-level build script.
49
50
51## Building the First Page
52
531. Use the **\<Text>** component.
54
55   After the project synchronization is complete, choose **entry** > **src** > **main** > **js** > **MainAbility** > **pages** > **index** in the **Project** window and open the **index.hml** file. You can see that the file contains a **<Text>** component. The sample code in the **index.hml** file is shown below:
56
57   ```html
58   <!-- index.hml -->
59   <div class="container">
60       <text class="title">
61           Hello World
62       </text>
63   </div>
64   ```
65
662. Add a button and bind the **onclick** method to this button.
67
68   On the default page, add a **\<Button>** component to respond to user clicks and implement redirection to another page. The sample code in the **index.hml** file is shown below:
69
70   ```html
71   <!-- index.hml -->
72   <div class="container">
73       <text class="title">
74           Hello World
75       </text>
76
77   <!-- Add a button, set its value to Next, and bind the onclick method to the button. -->
78       <input class="btn" type="button" value="Next" onclick="onclick"></input>
79   </div>
80   ```
81
823. Set the page style in the **index.css** file.
83
84   From the **Project** window, choose **entry** > **src** > **main** > **js** > **MainAbility** > **pages** > **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:
85
86   ```css
87   /* index.css */
88   .container {
89       display: flex;
90       flex-direction: column;
91       justify-content: center;
92       align-items: center;
93       left: 0px;
94       top: 0px;
95       width: 100%;
96       height: 100%;
97   }
98
99   .title {
100       font-size: 100px;
101       font-weight: bold;
102       text-align: center;
103       width: 100%;
104       margin: 10px;
105   }
106
107   .btn {
108       font-size: 60px;
109       font-weight: bold;
110       text-align: center;
111       width: 40%;
112       height: 5%;
113       margin-top: 20px;
114   }
115   ```
116
1174. On the toolbar in the upper right corner of the editing window, click **Previewer**. Below is how the first page looks in the Previewer.
118
119   ![en-us_image_0000001311494592](figures/en-us_image_0000001311494592.png)
120
121
122## Building the Second Page
123
1241. Create the second page.
125
126   In the **Project** window, choose **entry** > **src** > **main** > **js** > **MainAbility**, right-click the **pages** folder, choose **New** > **Page**, name the page **second**, and click **Finish**. Below is the structure of the **second** folder.
127
128   ![en-us_image_0000001311334944](figures/en-us_image_0000001311334944.png)
129
1302. Add **\<Text>** and **\<Button>** components.
131
132   Add **\<Text>** and **\<Button>** components and set their styles, as you do for the first page. The sample code in the **second.hml** file is shown below:
133
134   ```html
135   <!-- second.hml -->
136   <div class="container">
137       <text class="title">
138           Hi there
139       </text>
140
141   <!-- Add a button, set the value to Back, and bind the back method to the button.-->
142       <input class="btn" type="button" value="Back" onclick="back"></input>
143   </div>
144   ```
145
1463. Set the page style. The sample code in the **second.css** file is shown below:
147
148   ```css
149   /* second.css */
150   .container {
151       display: flex;
152       flex-direction: column;
153       justify-content: center;
154       align-items: center;
155       left: 0px;
156       top: 0px;
157       width: 100%;
158       height: 100%;
159   }
160
161   .title {
162       font-size: 100px;
163       font-weight: bold;
164       text-align: center;
165       width: 100%;
166       margin: 10px;
167   }
168
169   .btn {
170       font-size: 60px;
171       font-weight: bold;
172       text-align: center;
173       width: 40%;
174       height: 5%;
175       margin-top: 20px;
176   }
177   ```
178
179
180## Implementing Page Redirection
181
182You can implement page redirection through the [page router](../reference/apis/js-apis-router.md), which finds the target page based on the page URL. Import the **router** module and then perform the steps below:
183
1841. Implement redirection from the first page to the second page.
185
186   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:
187
188   ```js
189   // index.js
190   // Import the router module.
191   import router from '@ohos.router';
192
193   export default {
194       onclick: function () {
195           router.push({
196               url: "pages/second/second"
197           })
198       }
199   }
200   ```
201
2022. Implement redirection from the second page to the first page.
203
204   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:
205
206   ```js
207   // second.js
208   // Import the router module.
209   import router from '@ohos.router';
210
211   export default {
212       back: function () {
213           router.back()
214       }
215   }
216   ```
217
2183. Open any file in the **index** folder and click ![en-us_image_0000001311015192](figures/en-us_image_0000001311015192.png) in the Previewer to refresh the file. The display effect is shown in the figure below.
219
220   ![en-us_image_0000001311175132](figures/en-us_image_0000001311175132.png)
221
222
223## Running the Application on a Real Device
224
2251. Connect the development board running the OpenHarmony standard system to the computer.
226
2272. Choose **File** > **Project Structure...** > **Project** > **Signing Configs**, and select **Automatically generate signature**. Wait until the automatic signing is complete, and click **OK**. See the following figure.
228
229   ![06](figures/06.png)
230
2313. On the toolbar in the upper right corner of the editing window, click ![en-us_image_0000001364054485](figures/en-us_image_0000001364054485.png). The display effect is shown in the figure below.
232
233   ![en-us_image_0000001311175132](figures/en-us_image_0000001311175132.png)
234
235Congratulations! You have finished developing your OpenHarmony application in JavaScript in the FA model. To learn more about OpenHarmony application development, see [Application Development Overview](../application-dev-guide.md).
236