start-with-ets-fa.md 11.0 KB
Newer Older
E
ester.zhou 已提交
1
# Getting Started with ArkTS in FA Model
G
ge-yafang 已提交
2

E
ester.zhou 已提交
3 4 5

>  **NOTE**
>
E
ester.zhou 已提交
6
>  To use ArkTS, your DevEco Studio must be V3.0.0.601 Beta1 or later.
E
ester.zhou 已提交
7
>
E
ester.zhou 已提交
8
>  For best possible results, use [DevEco Studio 3.1 Beta1](https://developer.harmonyos.com/cn/develop/deveco-studio) for your development.
G
ge-yafang 已提交
9 10


E
ester.zhou 已提交
11
## Creating an ArkTS Project
G
ge-yafang 已提交
12

E
ester.zhou 已提交
13 14 15
1. 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**.

   ![01](figures/01.png)
16

E
ester.zhou 已提交
17
2. 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.
G
ge-yafang 已提交
18

E
ester.zhou 已提交
19
   ![02](figures/02.png)
20

E
ester.zhou 已提交
21
   > **NOTE**
E
ester.zhou 已提交
22
   > 
E
ester.zhou 已提交
23
   > If you are using DevEco Studio V3.0 Beta3 or later, you can use the [low-code development](https://developer.harmonyos.com/en/docs/documentation/doc-guides/ohos-low-code-development-0000001218440652) mode apart from the traditional coding approach.
E
ester.zhou 已提交
24
   > 
E
ester.zhou 已提交
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.
E
ester.zhou 已提交
26
   > 
E
ester.zhou 已提交
27
   > To use the low-code development mode, turn on **Enable Super Visual** on the page shown above.
G
ge-yafang 已提交
28 29 30 31

3. Click **Finish**. DevEco Studio will automatically generate the sample code and resources that match your project type. Wait until the project is created.


E
ester.zhou 已提交
32 33 34
## ArkTS Project Directory Structure (FA Model)

![en-us_image_0000001384652328](figures/en-us_image_0000001384652328.png)
G
ge-yafang 已提交
35

E
ester.zhou 已提交
36 37 38 39 40 41
- **entry**: OpenHarmony project module, which can be built into an OpenHarmony Ability Package ([HAP](../../glossary.md#hap)).
  - **src > main > ets**: a collection of eTS source code.
  - **src > main > ets > MainAbility**: entry to your application/service.
  - **src > main > ets > MainAbility > pages**: pages contained in **MainAbility**.
  - **src > main > ets > MainAbility > pages > index.ets**: the first page in the **pages** list, also referred to as the entry to the application.
  - **src > main > ets > MainAbility > app.ets**: ability lifecycle file.
E
ester.zhou 已提交
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).
E
ester.zhou 已提交
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**.
E
ester.zhou 已提交
45
  - **hvigorfile.ts**: module-level build script. You can customize related tasks and code implementation.
E
ester.zhou 已提交
46

E
ester.zhou 已提交
47
- **build-profile.json5**: application-level configuration information, including the signature and product configuration.
E
ester.zhou 已提交
48

E
ester.zhou 已提交
49
- **hvigorfile.ts**: application-level build script.
G
ge-yafang 已提交
50 51 52 53


## Building the First Page

E
ester.zhou 已提交
54
1. Use the **\<Text>** component.
G
ge-yafang 已提交
55

E
ester.zhou 已提交
56 57
   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:
   
58 59
   ```ts
   // index.ets
G
ge-yafang 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
   @Entry
   @Component
   struct Index {
     @State message: string = 'Hello World'
   
     build() {
       Row() {
         Column() {
           Text(this.message)
             .fontSize(50)
             .fontWeight(FontWeight.Bold)
         }
         .width('100%')
       }
       .height('100%')
     }
   }
   ```

E
ester.zhou 已提交
79
2. Add a **\<Button>** component.
G
ge-yafang 已提交
80

E
ester.zhou 已提交
81 82
   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:
   
83 84
   ```ts
   // index.ets
G
ge-yafang 已提交
85 86 87 88 89 90 91 92 93 94 95
   @Entry
   @Component
   struct Index {
     @State message: string = 'Hello World'
   
     build() {
       Row() {
         Column() {
           Text(this.message)
             .fontSize(50)
             .fontWeight(FontWeight.Bold)
E
ester.zhou 已提交
96
           // Add a button to respond to user clicks.
G
ge-yafang 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
           Button() {
             Text('Next')
               .fontSize(30)
               .fontWeight(FontWeight.Bold)
           }
           .type(ButtonType.Capsule)
           .margin({
             top: 20
           })
           .backgroundColor('#0D9FFB')
           .width('40%')
           .height('5%')
         }
         .width('100%')
       }
       .height('100%')
     }
   }
   ```

E
ester.zhou 已提交
117
3. On the toolbar in the upper right corner of the editing window, click **Previewer**. Below is how the first page looks in the Previewer.
E
ester.zhou 已提交
118

E
ester.zhou 已提交
119
   ![en-us_image_0000001311334976](figures/en-us_image_0000001311334976.png)
G
ge-yafang 已提交
120 121 122 123 124 125


## Building the Second Page

1. Create the second page.

E
ester.zhou 已提交
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.
E
ester.zhou 已提交
127 128 129
      ![en-us_image_0000001311334932](figures/en-us_image_0000001311334932.png)

      > **NOTE**
E
ester.zhou 已提交
130
      > 
E
ester.zhou 已提交
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.
E
ester.zhou 已提交
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:
E
ester.zhou 已提交
133 134 135 136 137 138 139 140 141
     
      ```json
      {
        "module": {
          "js": [
            {
              "pages": [
                "pages/index",
                "pages/second"
E
ester.zhou 已提交
142
              ]
E
ester.zhou 已提交
143 144 145 146 147 148 149 150 151 152
              }
          ]
        }
      }
      ```

2. Add **\<Text>** and **\<Button>** components.

   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:
   
153 154
   ```ts
   // second.ets
G
ge-yafang 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
   @Entry
   @Component
   struct Second {
     @State message: string = 'Hi there'
   
     build() {
       Row() {
         Column() {
           Text(this.message)
             .fontSize(50)
             .fontWeight(FontWeight.Bold)
           Button() {
             Text('Back')
               .fontSize(25)
               .fontWeight(FontWeight.Bold)
           }
           .type(ButtonType.Capsule)
           .margin({
             top: 20
           })
           .backgroundColor('#0D9FFB')
           .width('40%')
           .height('5%')
         }
         .width('100%')
       }
       .height('100%')
     }
   }
   ```


## Implementing Page Redirection

E
ester.zhou 已提交
189
You 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:
G
ge-yafang 已提交
190 191 192

1. Implement redirection from the first page to the second page.

E
ester.zhou 已提交
193 194
   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:
   
195 196
   ```ts
   // index.ets
E
ester.zhou 已提交
197
   // Import the router module.
198
   import router from '@ohos.router';
G
ge-yafang 已提交
199 200 201 202 203 204 205 206 207 208 209 210
   
   @Entry
   @Component
   struct Index {
     @State message: string = 'Hello World'
   
     build() {
       Row() {
         Column() {
           Text(this.message)
             .fontSize(50)
             .fontWeight(FontWeight.Bold)
E
ester.zhou 已提交
211
           // Add a button to respond to user clicks.
G
ge-yafang 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225
           Button() {
             Text('Next')
               .fontSize(30)
               .fontWeight(FontWeight.Bold)
           }
           .type(ButtonType.Capsule)
           .margin({
             top: 20
           })
           .backgroundColor('#0D9FFB')
           .width('40%')
           .height('5%')
           // Bind the onClick event to the Next button so that clicking the button redirects the user to the second page.
           .onClick(() => {
E
ester.zhou 已提交
226 227 228 229
             router.push({ url: 'pages/second' })
             // In a project of API version 9, you can use the API below instead:
             // router.pushUrl({ url: 'pages/second' })

G
ge-yafang 已提交
230 231 232 233 234 235 236 237 238 239
           })
         }
         .width('100%')
       }
       .height('100%')
     }
   }
   ```

2. Implement redirection from the second page to the first page.
E
ester.zhou 已提交
240

G
ge-yafang 已提交
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:
E
ester.zhou 已提交
242
   
243 244
   ```ts
   // second.ets
E
ester.zhou 已提交
245
   // Import the router module.
246
   import router from '@ohos.router';
G
ge-yafang 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
   
   @Entry
   @Component
   struct Second {
     @State message: string = 'Hi there'
   
     build() {
       Row() {
         Column() {
           Text(this.message)
             .fontSize(50)
             .fontWeight(FontWeight.Bold)
           Button() {
             Text('Back')
               .fontSize(25)
               .fontWeight(FontWeight.Bold)
           }
           .type(ButtonType.Capsule)
           .margin({
             top: 20
           })
           .backgroundColor('#0D9FFB')
           .width('40%')
           .height('5%')
           // Bind the onClick event to the Back button so that clicking the button redirects the user back to the first page.
           .onClick(() => {
             router.back()
           })
         }
         .width('100%')
       }
       .height('100%')
     }
   }
   ```

E
ester.zhou 已提交
283
3. 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

E
ester.zhou 已提交
285
   ![en-us_image_0000001364254729](figures/en-us_image_0000001364254729.png)
G
ge-yafang 已提交
286 287 288 289 290 291


## Running the Application on a Real Device

1. Connect the development board running the OpenHarmony standard system to the computer.

E
ester.zhou 已提交
292
2. 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

E
ester.zhou 已提交
294
   ![06](figures/06.png)
G
ge-yafang 已提交
295

E
ester.zhou 已提交
296
3. 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

E
ester.zhou 已提交
298
   ![en-us_image_0000001364254729](figures/en-us_image_0000001364254729.png)
G
ge-yafang 已提交
299

E
ester.zhou 已提交
300
Congratulations! 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).