• 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 3.1 Beta2](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 **Choose Your Ability Template** page, select **Application** (or **Atomic Service**, depending on your project), select **Empty Ability** as the template, and click Next.
12
13   ![createProject](figures/createProject.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   ![chooseFAModel_js](figures/chooseFAModel_js.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
294. After the project is created, in the **entry** > **build-profile.json5** file, change **runtimeOS** under **targets** to **OpenHarmony**, and click **Sync Now** in the upper right corner to start development.
30
31
32## JavaScript Project Directory Structure
33
34![en-us_image_0000001435376433](figures/en-us_image_0000001435376433.png)
35
36- **entry**: OpenHarmony project module, which can be built into an OpenHarmony Ability Package ([HAP](../../glossary.md#hap)).
37  - **src > main > js**: a collection of JavaScript source code.
38  - **src > main > js > MainAbility**: entry to your application/service.
39  - **src > main > js > MainAbility > i18n**: resources in different languages, for example, UI strings and image paths.
40  - **src > main > js > MainAbility > pages**: pages contained in **MainAbility**.
41  - **src > main > js > MainAbility > app.js**: ability lifecycle file.
42
43  - **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).
44  - **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).
45  - **build-profile.json5**: current module information and build configuration options, including **buildOption** and **targets**. Under **targets**, you can set **runtimeOS** to **HarmonyOS** (default) or **OpenHarmony**, depending on the OS of your application.
46  - **hvigorfile.ts**: module-level build script. You can customize related tasks and code implementation.
47
48- **build-profile.json5**: application-level configuration information, including the signature and product configuration.
49
50- **hvigorfile.ts**: application-level build script.
51
52
53## Building the First Page
54
551. Use the **\<Text>** component.
56
57   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:
58
59   ```html
60   <!-- index.hml -->
61   <div class="container">
62       <text class="title">
63           Hello World
64       </text>
65   </div>
66   ```
67
682. Add a button and bind the **onclick** method to this button.
69
70   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:
71
72   ```html
73   <!-- index.hml -->
74   <div class="container">
75       <text class="title">
76           Hello World
77       </text>
78
79   <!-- Add a button, set its value to Next, and bind the onclick method to the button. -->
80       <input class="btn" type="button" value="Next" onclick="onclick"></input>
81   </div>
82   ```
83
843. Set the page style in the **index.css** file.
85
86   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:
87
88   ```css
89   /* index.css */
90   .container {
91       display: flex;
92       flex-direction: column;
93       justify-content: center;
94       align-items: center;
95       left: 0px;
96       top: 0px;
97       width: 100%;
98       height: 100%;
99   }
100
101   .title {
102       font-size: 100px;
103       font-weight: bold;
104       text-align: center;
105       width: 100%;
106       margin: 10px;
107   }
108
109   .btn {
110       font-size: 60px;
111       font-weight: bold;
112       text-align: center;
113       width: 40%;
114       height: 5%;
115       margin-top: 20px;
116   }
117   ```
118
1194. On the toolbar in the upper right corner of the editing window, click **Previewer**. Below is how the first page looks in the Previewer.
120
121   ![en-us_image_0000001311494592](figures/en-us_image_0000001311494592.png)
122
123
124## Building the Second Page
125
1261. Create the second page.
127
128   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.
129
130   ![en-us_image_0000001311334944](figures/en-us_image_0000001311334944.png)
131
1322. Add **\<Text>** and **\<Button>** components.
133
134   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:
135
136   ```html
137   <!-- second.hml -->
138   <div class="container">
139       <text class="title">
140           Hi there
141       </text>
142
143   <!-- Add a button, set the value to Back, and bind the back method to the button.-->
144       <input class="btn" type="button" value="Back" onclick="back"></input>
145   </div>
146   ```
147
1483. Set the page style. The sample code in the **second.css** file is shown below:
149
150   ```css
151   /* second.css */
152   .container {
153       display: flex;
154       flex-direction: column;
155       justify-content: center;
156       align-items: center;
157       left: 0px;
158       top: 0px;
159       width: 100%;
160       height: 100%;
161   }
162
163   .title {
164       font-size: 100px;
165       font-weight: bold;
166       text-align: center;
167       width: 100%;
168       margin: 10px;
169   }
170
171   .btn {
172       font-size: 60px;
173       font-weight: bold;
174       text-align: center;
175       width: 40%;
176       height: 5%;
177       margin-top: 20px;
178   }
179   ```
180
181
182## Implementing Page Redirection
183
184You 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:
185
1861. Implement redirection from the first page to the second page.
187
188   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:
189
190   ```js
191   // index.js
192   // Import the router module.
193   import router from '@ohos.router';
194
195   export default {
196       onclick: function () {
197           router.push({
198               url: "pages/second/second"
199           })
200       }
201   }
202   ```
203
2042. Implement redirection from the second page to the first page.
205
206   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:
207
208   ```js
209   // second.js
210   // Import the router module.
211   import router from '@ohos.router';
212
213   export default {
214       back: function () {
215           router.back()
216       }
217   }
218   ```
219
2203. 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.
221
222   ![en-us_image_0000001311175132](figures/en-us_image_0000001311175132.png)
223
224
225## Running the Application on a Real Device
226
2271. Connect the development board running the OpenHarmony standard system to the computer.
228
2292. 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.
230
231   ![signConfig](figures/signConfig.png)
232
2333. 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.
234
235   ![en-us_image_0000001311175132](figures/en-us_image_0000001311175132.png)
236
237Congratulations! 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).
238