• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Getting Started with ArkTS in FA Model
2
3
4>  **NOTE**
5>
6>  To use ArkTS, your DevEco Studio must be V3.0.0.601 Beta1 or later.
7>
8>  For best possible results, use [DevEco Studio V3.1.0.100](https://developer.harmonyos.com/cn/develop/deveco-studio) for your development.
9
10
11## Creating an ArkTS Project
12
131. 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**.
14
15   ![01](figures/01.png)
16
172. 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 **ArkTS** and retain the default values for other parameters.
18
19   ![02](figures/02.png)
20
21   > **NOTE**
22   >
23   > If you are using DevEco Studio V3.0 Beta3 or later, you can use the low-code development mode apart from the traditional coding approach.
24   >
25   > 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.
26   >
27   > To use the low-code development mode, turn on **Enable Super Visual** on the page shown above.
28
293. Click **Finish**. DevEco Studio will automatically generate the sample code and resources that match your project type. Wait until the project is created.
30
31
32## ArkTS Project Directory Structure (FA Model)
33
34![en-us_image_0000001384652328](figures/en-us_image_0000001384652328.png)
35
36- **entry**: OpenHarmony project module, which can be built into an OpenHarmony Ability Package ([HAP](../../glossary.md#hap)).
37  - **src > main > ets**: a collection of ArkTS source code.
38  - **src > main > ets > MainAbility**: entry to your application/service.
39  - **src > main > ets > MainAbility > pages**: pages contained in **MainAbility**.
40  - **src > main > ets > MainAbility > pages > index.ets**: the first page in the **pages** list, also referred to as the entry to the application.
41  - **src > main > ets > MainAbility > app.ets**: ability lifecycle file.
42  - **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).
43  - **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).
44  - **build-profile.json5**: current module information and build configuration options, including **buildOption** and **targets**.
45  - **hvigorfile.ts**: module-level build script. You can customize related tasks and code implementation.
46
47- **build-profile.json5**: application-level configuration information, including the signature and product configuration.
48
49- **hvigorfile.ts**: application-level build script.
50
51
52## Building the First Page
53
541. Use the **\<Text>** component.
55
56   After the project synchronization is complete, choose **entry** > **src** > **main** > **ets** > **MainAbility** > **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:
57
58   ```ts
59   // index.ets
60   @Entry
61   @Component
62   struct Index {
63     @State message: string = 'Hello World'
64
65     build() {
66       Row() {
67         Column() {
68           Text(this.message)
69             .fontSize(50)
70             .fontWeight(FontWeight.Bold)
71         }
72         .width('100%')
73       }
74       .height('100%')
75     }
76   }
77   ```
78
792. Add a **\<Button>** component.
80
81   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:
82
83   ```ts
84   // index.ets
85   @Entry
86   @Component
87   struct Index {
88     @State message: string = 'Hello World'
89
90     build() {
91       Row() {
92         Column() {
93           Text(this.message)
94             .fontSize(50)
95             .fontWeight(FontWeight.Bold)
96           // Add a button to respond to user clicks.
97           Button() {
98             Text('Next')
99               .fontSize(30)
100               .fontWeight(FontWeight.Bold)
101           }
102           .type(ButtonType.Capsule)
103           .margin({
104             top: 20
105           })
106           .backgroundColor('#0D9FFB')
107           .width('40%')
108           .height('5%')
109         }
110         .width('100%')
111       }
112       .height('100%')
113     }
114   }
115   ```
116
1173. 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_0000001311334976](figures/en-us_image_0000001311334976.png)
120
121
122## Building the Second Page
123
1241. Create the second page.
125
126   - Create the second page file: In the **Project** window, choose **entry** > **src** > **main** > **ets** > **MainAbility**. Right-click the **pages** folder, choose **New** > **ArkTS File**, name the page **second**, and click **Finish**. Below is the structure of the **second** folder.
127      ![en-us_image_0000001311334932](figures/en-us_image_0000001311334932.png)
128
129      > **NOTE**
130      >
131      > 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.
132   - Configure the route for the second page, by setting **pages/second** under **module - js - pages** in the **config.json** file. The sample code is as follows:
133
134      ```json
135      {
136        "module": {
137          "js": [
138            {
139              "pages": [
140                "pages/index",
141                "pages/second"
142              ]
143              }
144          ]
145        }
146      }
147      ```
148
1492. Add **\<Text>** and **\<Button>** components.
150
151   Add **\<Text>** and **\<Button>** components and set their styles, as you do for the first page. The sample code in the **second.ets** file is shown below:
152
153   ```ts
154   // second.ets
155   @Entry
156   @Component
157   struct Second {
158     @State message: string = 'Hi there'
159
160     build() {
161       Row() {
162         Column() {
163           Text(this.message)
164             .fontSize(50)
165             .fontWeight(FontWeight.Bold)
166           Button() {
167             Text('Back')
168               .fontSize(25)
169               .fontWeight(FontWeight.Bold)
170           }
171           .type(ButtonType.Capsule)
172           .margin({
173             top: 20
174           })
175           .backgroundColor('#0D9FFB')
176           .width('40%')
177           .height('5%')
178         }
179         .width('100%')
180       }
181       .height('100%')
182     }
183   }
184   ```
185
186
187## Implementing Page Redirection
188
189You 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:
190
1911. Implement redirection from the first page to the second page.
192
193   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:
194
195   ```ts
196   // index.ets
197   // Import the router module.
198   import router from '@ohos.router';
199
200   @Entry
201   @Component
202   struct Index {
203     @State message: string = 'Hello World'
204
205     build() {
206       Row() {
207         Column() {
208           Text(this.message)
209             .fontSize(50)
210             .fontWeight(FontWeight.Bold)
211           // Add a button to respond to user clicks.
212           Button() {
213             Text('Next')
214               .fontSize(30)
215               .fontWeight(FontWeight.Bold)
216           }
217           .type(ButtonType.Capsule)
218           .margin({
219             top: 20
220           })
221           .backgroundColor('#0D9FFB')
222           .width('40%')
223           .height('5%')
224           // Bind the onClick event to the Next button so that clicking the button redirects the user to the second page.
225           .onClick(() => {
226             router.push({ url: 'pages/second' })
227             // In a project of API version 9, you can use the API below instead:
228             // router.pushUrl({ url: 'pages/second' })
229
230           })
231         }
232         .width('100%')
233       }
234       .height('100%')
235     }
236   }
237   ```
238
2392. Implement redirection from the second page to the first page.
240
241   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:
242
243   ```ts
244   // second.ets
245   // Import the router module.
246   import router from '@ohos.router';
247
248   @Entry
249   @Component
250   struct Second {
251     @State message: string = 'Hi there'
252
253     build() {
254       Row() {
255         Column() {
256           Text(this.message)
257             .fontSize(50)
258             .fontWeight(FontWeight.Bold)
259           Button() {
260             Text('Back')
261               .fontSize(25)
262               .fontWeight(FontWeight.Bold)
263           }
264           .type(ButtonType.Capsule)
265           .margin({
266             top: 20
267           })
268           .backgroundColor('#0D9FFB')
269           .width('40%')
270           .height('5%')
271           // Bind the onClick event to the Back button so that clicking the button redirects the user back to the first page.
272           .onClick(() => {
273             router.back()
274           })
275         }
276         .width('100%')
277       }
278       .height('100%')
279     }
280   }
281   ```
282
2833. 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.
284
285   ![en-us_image_0000001364254729](figures/en-us_image_0000001364254729.png)
286
287
288## Running the Application on a Real Device
289
2901. Connect the development board running the OpenHarmony standard system to the computer.
291
2922. 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.
293
294   ![06](figures/06.png)
295
2963. 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.
297
298   ![en-us_image_0000001364254729](figures/en-us_image_0000001364254729.png)
299
300Congratulations! You have finished developing your OpenHarmony application in ArkTS in the FA model. To learn more about OpenHarmony application development, see [Application Development Overview](../application-dev-guide.md).
301