• 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 3.1 Beta2](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 **Choose Your Ability Template** page, select **Application** (or **Atomic Service**, depending on your project), select **Empty Ability** as the template, and click Next.
14
15   ![createProject](figures/createProject.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   ![chooseFAModel_ets](figures/chooseFAModel_ets.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.
304. 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.
31
32
33## ArkTS Project Directory Structure (FA Model)
34
35![en-us_image_0000001384652328](figures/en-us_image_0000001384652328.png)
36
37- **entry**: OpenHarmony project module, which can be built into an OpenHarmony Ability Package ([HAP](../../glossary.md#hap)).
38  - **src > main > ets**: a collection of ArkTS source code.
39  - **src > main > ets > MainAbility**: entry to your application/service.
40  - **src > main > ets > MainAbility > pages**: pages contained in **MainAbility**.
41  - **src > main > ets > MainAbility > pages > index.ets**: the first page in the **pages** list, also referred to as the entry to the application.
42  - **src > main > ets > MainAbility > app.ets**: ability lifecycle file.
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 Categories and Access](resource-categories-and-access.md#resource-categories).
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- **build-profile.json5**: application-level configuration information, including the signature and product configuration.
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** > **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:
56
57   ```ts
58   // index.ets
59   @Entry
60   @Component
61   struct Index {
62     @State message: string = 'Hello World'
63
64     build() {
65       Row() {
66         Column() {
67           Text(this.message)
68             .fontSize(50)
69             .fontWeight(FontWeight.Bold)
70         }
71         .width('100%')
72       }
73       .height('100%')
74     }
75   }
76   ```
77
782. Add a **\<Button>** component.
79
80   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:
81
82   ```ts
83   // index.ets
84   @Entry
85   @Component
86   struct Index {
87     @State message: string = 'Hello World'
88
89     build() {
90       Row() {
91         Column() {
92           Text(this.message)
93             .fontSize(50)
94             .fontWeight(FontWeight.Bold)
95           // Add a button to respond to user clicks.
96           Button() {
97             Text('Next')
98               .fontSize(30)
99               .fontWeight(FontWeight.Bold)
100           }
101           .type(ButtonType.Capsule)
102           .margin({
103             top: 20
104           })
105           .backgroundColor('#0D9FFB')
106           .width('40%')
107           .height('5%')
108         }
109         .width('100%')
110       }
111       .height('100%')
112     }
113   }
114   ```
115
1163. On the toolbar in the upper right corner of the editing window, click **Previewer**. Below is how the first page looks in the Previewer.
117
118   ![en-us_image_0000001311334976](figures/en-us_image_0000001311334976.png)
119
120
121## Building the Second Page
122
1231. Create the second page.
124
125   - 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.
126      ![en-us_image_0000001311334932](figures/en-us_image_0000001311334932.png)
127
128      > **NOTE**
129      >
130      > 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.
131   - 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:
132
133      ```json
134      {
135        "module": {
136          "js": [
137            {
138              "pages": [
139                "pages/index",
140                "pages/second"
141              ]
142              }
143          ]
144        }
145      }
146      ```
147
1482. Add **\<Text>** and **\<Button>** components.
149
150   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:
151
152   ```ts
153   // second.ets
154   @Entry
155   @Component
156   struct Second {
157     @State message: string = 'Hi there'
158
159     build() {
160       Row() {
161         Column() {
162           Text(this.message)
163             .fontSize(50)
164             .fontWeight(FontWeight.Bold)
165           Button() {
166             Text('Back')
167               .fontSize(25)
168               .fontWeight(FontWeight.Bold)
169           }
170           .type(ButtonType.Capsule)
171           .margin({
172             top: 20
173           })
174           .backgroundColor('#0D9FFB')
175           .width('40%')
176           .height('5%')
177         }
178         .width('100%')
179       }
180       .height('100%')
181     }
182   }
183   ```
184
185
186## Implementing Page Redirection
187
188You 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:
189
1901. Implement redirection from the first page to the second page.
191
192   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:
193
194   ```ts
195   // index.ets
196   // Import the router module.
197   import router from '@ohos.router';
198
199   @Entry
200   @Component
201   struct Index {
202     @State message: string = 'Hello World'
203
204     build() {
205       Row() {
206         Column() {
207           Text(this.message)
208             .fontSize(50)
209             .fontWeight(FontWeight.Bold)
210           // Add a button to respond to user clicks.
211           Button() {
212             Text('Next')
213               .fontSize(30)
214               .fontWeight(FontWeight.Bold)
215           }
216           .type(ButtonType.Capsule)
217           .margin({
218             top: 20
219           })
220           .backgroundColor('#0D9FFB')
221           .width('40%')
222           .height('5%')
223           // Bind the onClick event to the Next button so that clicking the button redirects the user to the second page.
224           .onClick(() => {
225             router.push({ url: 'pages/second' })
226             // In a project of API version 9, you can use the API below instead:
227             // router.pushUrl({ url: 'pages/second' })
228
229           })
230         }
231         .width('100%')
232       }
233       .height('100%')
234     }
235   }
236   ```
237
2382. Implement redirection from the second page to the first page.
239
240   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:
241
242   ```ts
243   // second.ets
244   // Import the router module.
245   import router from '@ohos.router';
246
247   @Entry
248   @Component
249   struct Second {
250     @State message: string = 'Hi there'
251
252     build() {
253       Row() {
254         Column() {
255           Text(this.message)
256             .fontSize(50)
257             .fontWeight(FontWeight.Bold)
258           Button() {
259             Text('Back')
260               .fontSize(25)
261               .fontWeight(FontWeight.Bold)
262           }
263           .type(ButtonType.Capsule)
264           .margin({
265             top: 20
266           })
267           .backgroundColor('#0D9FFB')
268           .width('40%')
269           .height('5%')
270           // Bind the onClick event to the Back button so that clicking the button redirects the user back to the first page.
271           .onClick(() => {
272             router.back()
273           })
274         }
275         .width('100%')
276       }
277       .height('100%')
278     }
279   }
280   ```
281
2823. 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.
283
284   ![en-us_image_0000001364254729](figures/en-us_image_0000001364254729.png)
285
286
287## Running the Application on a Real Device
288
2891. Connect the development board running the OpenHarmony standard system to the computer.
290
2912. 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.
292
293   ![signConfig](figures/signConfig.png)
294
2953. 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.
296
297   ![en-us_image_0000001364254729](figures/en-us_image_0000001364254729.png)
298
299Congratulations! 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).
300