• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Building the First ArkTS Application in Stage Model
2
3
4> **NOTE**
5>
6> In this document, DevEco Studio 4.0 Beta2 is used. You can download it [here](../../release-notes/OpenHarmony-v4.0-beta2.md#version-mapping).
7
8## Creating an ArkTS Project
9
10The procedure for creating a project varies, depending on whether the API version is later than 9 or not.
11
12The following describes how to create the OpenHarmony projects of API 10 and API 9.
13
14### Creating a Project of API Version 10
15
161. 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.
17
182. 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**.
19
20   ![createProject](figures/createProject.png)
21
223. On the project configuration page, set **Compile SDK** to **3.1.0(API 9)** and retain the default values for other parameters.
23
24   The **Node** parameter sets the Node.js version to use for the project. You can use an existing version or download a new one.
25
26   ![chooseStageModel](figures/chooseStageModel.png)
27
28   > **NOTE**
29   >
30   > You can use the low-code development mode apart from the traditional coding approach.
31   >
32   > 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.
33   >
34   > To use the low-code development mode, turn on **Enable Super Visual** on the page shown above.
35
364. Click **Finish**. DevEco Studio will automatically generate the sample code and resources that match your project type. Wait until the project is created.
37
385. After the project is created, in the project-level **build-profile.json5** file (at the same directory level as **entry**), move the **compileSdkVersion** and **compatibleSdkVersion** fields from under **app** to under the current **products**. You can identify the current **products** by clicking the ![en-us_image_0000001609333677](figures/en-us_image_0000001609333677.png) icon in the upper right corner of the editing area.
39
40   ![changeToAPI10](figures/changeToAPI10.png)
41
426. Change the value of **targetSdkVersion** from **9** to **10** and set **runtimeOS** to **OpenHarmony**.
43
44   ![targetSdkVersion](figures/targetSdkVersion.png)
45
467. Delete the **runtimeOS** configuration from the **targets** field in the module-level **build-profile.json5** files.
47
48   ![deleteRuntimeOS](figures/deleteRuntimeOS.png)
49
508. Click **Sync Now** and wait until the synchronization is complete. A project of API version 10 is now created.
51
52
53### Creating a Project of API Version 9
54
551. 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.
56
572. 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**.
58
59   ![createProject](figures/createProject.png)
60
613. On the project configuration page, set **Compile SDK** to **3.1.0(API 9)** and retain the default values for other parameters.
62
63   The **Node** parameter sets the Node.js version to use for the project. You can use an existing version or download a new one.
64
65   ![chooseStageModel](figures/chooseStageModel.png)
66
67   > **NOTE**
68   >
69   > You can use the low-code development mode apart from the traditional coding approach.
70   >
71   > 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.
72   >
73   > To use the low-code development mode, turn on **Enable Super Visual** on the page shown above.
74
754. Click **Finish**. DevEco Studio will automatically generate the sample code and resources that match your project type. Wait until the project is created.
76
775. In the module-level **entry** > **build-profile.json5** file, set **runtimeOS** in **targets** to **OpenHarmony**.
78
796. Click **Sync Now** and wait until the synchronization is complete. A project of API version 9 is now created.
80
81
82## ArkTS Project Directory Structure (Stage Model, API Version 10)
83
84![project](figures/project.png)
85
86- **AppScope > app.json5**: application-level configuration information.
87
88- **entry**: OpenHarmony project module, which can be built into an ability package (HAP).
89  - **src > main > ets**: a collection of ArkTS source code.
90
91  - **src > main > ets > entryability**: entry to your application/service.
92
93  - **src > main > ets > pages**: pages included in your application/service.
94
95  - **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 Categories and Access](resource-categories-and-access.md#resource-categories).
96
97  - **src > main > module.json5**: 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 [module.json5 Configuration File](module-configuration-file.md).
98
99  - **build-profile.json5**: current module information and build configuration options, including **buildOption** and **targets**.
100
101  - **hvigorfile.ts**: module-level build script. You can customize related tasks and code implementation in this file.
102
103- **oh_modules**: third-party library dependency information. For details about how to adapt a historical npm project to ohpm, see [Manually Migrating Historical Projects](https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/project_overview-0000001053822398-V3#section108143331212).
104
105- **build-profile.json5**: application-level configuration information, including the **signingConfigs** and **products** configuration. The **runtimeOS** field in **products** indicates the runtime OS. Its default value is **HarmonyOS**. If you are developing an OpenHarmony application, change the value to **OpenHarmony**.
106
107- **hvigorfile.ts**: application-level build script.
108
109
110## ArkTS Project Directory Structure (Stage Model, API Version 9)
111
112![project](figures/project.png)
113
114- **AppScope > app.json5**: application-level configuration information.
115
116- **entry**: OpenHarmony project module, which can be built into an ability package (HAP).
117  - **src > main > ets**: a collection of ArkTS source code.
118
119  - **src > main > ets > entryability**: entry to your application/service.
120
121  - **src > main > ets > pages**: pages included in your application/service.
122
123  - **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 Categories and Access](resource-categories-and-access.md#resource-categories).
124
125  - **src > main > module.json5**: 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 [module.json5 Configuration File](module-configuration-file.md).
126
127  - **build-profile.json5**: current module information and build configuration options, including **buildOption** and **targets**. The **runtimeOS** field in **targets** indicates the runtime OS. Its default value is **HarmonyOS**. If you are developing an OpenHarmony application, change the value to **OpenHarmony**.
128
129  - **hvigorfile.ts**: module-level build script. You can customize related tasks and code implementation in this file.
130
131- **oh_modules**: third-party library dependency information. For details about how to adapt a historical npm project to ohpm, see [Manually Migrating Historical Projects](https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/project_overview-0000001053822398-V3#section108143331212).
132
133- **build-profile.json5**: application-level configuration information, including the **signingConfigs** and **products** configuration.
134
135- **hvigorfile.ts**: application-level build script.
136
137
138## Building the First Page
139
1401. Use the **\<Text>** component.
141
142   After the project synchronization is complete, choose **entry** > **src** > **main** > **ets** > **pages** in the **Project** window and open the **Index.ets** file. You can see that the file contains a **\<Text>** component. The sample code in the **Index.ets** file is shown below:
143
144   ```ts
145   // Index.ets
146   @Entry
147   @Component
148   struct Index {
149     @State message: string = 'Hello World'
150
151     build() {
152       Row() {
153         Column() {
154           Text(this.message)
155             .fontSize(50)
156             .fontWeight(FontWeight.Bold)
157         }
158         .width('100%')
159       }
160       .height('100%')
161     }
162   }
163   ```
164
1652. Add a **\<Button>** component.
166
167   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.ets** file is shown below:
168
169   ```ts
170   // Index.ets
171   @Entry
172   @Component
173   struct Index {
174     @State message: string = 'Hello World'
175
176     build() {
177       Row() {
178         Column() {
179           Text(this.message)
180             .fontSize(50)
181             .fontWeight(FontWeight.Bold)
182           // Add a button to respond to user clicks.
183           Button() {
184             Text('Next')
185               .fontSize(30)
186               .fontWeight(FontWeight.Bold)
187           }
188           .type(ButtonType.Capsule)
189           .margin({
190             top: 20
191           })
192           .backgroundColor('#0D9FFB')
193           .width('40%')
194           .height('5%')
195         }
196         .width('100%')
197       }
198       .height('100%')
199     }
200   }
201   ```
202
2033. On the toolbar in the upper right corner of the editing window, click **Previewer**. Below is how the first page looks in the Previewer.
204
205   ![en-us_image_0000001311334976](figures/en-us_image_0000001311334976.png)
206
207
208## Building the Second Page
209
2101. Create the second page.
211
212   - Create the second page file: In the **Project** window, choose **entry** > **src** > **main** > **ets**. Right-click the **pages** folder, choose **New** > **ArkTS File**, name the page **Second**, and click **Finish**. Below is the structure of the **Second** folder.
213
214      ![secondPage](figures/secondPage.png)
215
216      >  **NOTE**
217      >
218      > You can also right-click the **pages** folder and choose **New** > **Page** from the shortcut menu. In this scenario, you do not need to manually configure page routes.
219   - Configure the route for the second page: In the **Project** window, choose **entry** > **src** > **main** > **resources** > **base** > **profile**. In the **main_pages.json** file, set **pages/Second** under **src**. The sample code is as follows:
220
221      ```json
222      {
223        "src": [
224          "pages/Index",
225          "pages/Second"
226        ]
227      }
228      ```
229
2302. Add **\<Text>** and **\<Button>** components.
231
232   Add **\<Text>** and **\<Button>** components and set their styles, by referring to the first page. The sample code in the **Second.ets** file is shown below:
233
234   ```ts
235   // Second.ets
236   @Entry
237   @Component
238   struct Second {
239     @State message: string = 'Hi there'
240
241     build() {
242       Row() {
243         Column() {
244           Text(this.message)
245             .fontSize(50)
246             .fontWeight(FontWeight.Bold)
247           Button() {
248             Text('Back')
249               .fontSize(25)
250               .fontWeight(FontWeight.Bold)
251           }
252           .type(ButtonType.Capsule)
253           .margin({
254             top: 20
255           })
256           .backgroundColor('#0D9FFB')
257           .width('40%')
258           .height('5%')
259         }
260         .width('100%')
261       }
262       .height('100%')
263     }
264   }
265   ```
266
267
268## Implementing Page Redirection
269
270You 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:
271
2721. Implement redirection from the first page to the second page.
273
274   In the **Index.ets** file of the first page, bind the **onClick** event to the **Next** button so that clicking the button redirects the user to the second page. The sample code in the **Index.ets** file is shown below:
275
276   ```ts
277   // Index.ets
278   // Import the router module.
279   import router from '@ohos.router';
280   import { BusinessError } from '@ohos.base';
281
282   @Entry
283   @Component
284   struct Index {
285     @State message: string = 'Hello World'
286
287     build() {
288       Row() {
289         Column() {
290           Text(this.message)
291             .fontSize(50)
292             .fontWeight(FontWeight.Bold)
293           // Add a button to respond to user clicks.
294           Button() {
295             Text('Next')
296               .fontSize(30)
297               .fontWeight(FontWeight.Bold)
298           }
299           .type(ButtonType.Capsule)
300           .margin({
301             top: 20
302           })
303           .backgroundColor('#0D9FFB')
304           .width('40%')
305           .height('5%')
306           // Bind the onClick event to the Next button so that clicking the button redirects the user to the second page.
307           .onClick(() => {
308             console.info(`Succeeded in clicking the 'Next' button.`)
309            // Go to the second page.
310              router.pushUrl({ url: 'pages/Second' }).then(() => {
311                console.info('Succeeded in jumping to the second page.')
312              }).catch((err: BusinessError) => {
313                console.error(`Failed to jump to the second page.Code is ${err.code}, message is ${err.message}`)
314              })
315           })
316         }
317         .width('100%')
318       }
319       .height('100%')
320     }
321   }
322   ```
323
3242. Implement redirection from the second page to the first page.
325
326   In the **Second.ets** file of the second page, bind the **onClick** event to the **Back** button so that clicking the button redirects the user back to the first page. The sample code in the **Second.ets** file is shown below:
327
328   ```ts
329   // Second.ets
330   // Import the router module.
331   import router from '@ohos.router';
332   import { BusinessError } from '@ohos.base';
333
334   @Entry
335   @Component
336   struct Second {
337     @State message: string = 'Hi there'
338
339     build() {
340       Row() {
341         Column() {
342           Text(this.message)
343             .fontSize(50)
344             .fontWeight(FontWeight.Bold)
345           Button() {
346             Text('Back')
347               .fontSize(25)
348               .fontWeight(FontWeight.Bold)
349           }
350           .type(ButtonType.Capsule)
351           .margin({
352             top: 20
353           })
354           .backgroundColor('#0D9FFB')
355           .width('40%')
356           .height('5%')
357           // Bind the onClick event to the Back button so that clicking the button redirects the user back to the first page.
358           .onClick(() => {
359             console.info(`Succeeded in clicking the 'Back' button.`)
360             try {
361               // Return to the first page.
362               router.back()
363               console.info('Succeeded in returning to the first page.')
364             } catch (err) {
365                let code = (err as BusinessError).code;
366                let message = (err as BusinessError).message;
367               console.error(`Failed to return to the first page.Code is ${code}, message is ${message}`)
368             }
369           })
370         }
371         .width('100%')
372       }
373       .height('100%')
374     }
375   }
376   ```
377
3783. Open the **Index.ets** file 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.
379
380   ![en-us_image_0000001364254729](figures/en-us_image_0000001364254729.png)
381
382
383## Running the Application on a Real Device
384
3851. Connect the development board running the OpenHarmony standard system to the computer.
386
3872. Choose **File** > **Project Structure...** > **Project** > **SigningConfigs**, and select **Automatically generate signature**. Wait until the automatic signing is complete, and click **OK**. See the following figure.
388
389   ![signConfig](figures/signConfig.png)
390
3913. 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.
392
393   ![en-us_image_0000001364254729](figures/en-us_image_0000001364254729.png)
394
395Congratulations! You have finished developing your OpenHarmony application in ArkTS in the stage model. To learn more about OpenHarmony application development, see [Application Development Overview](../application-dev-guide.md).
396