• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Getting Started with ArkTS in Stage Model
2
3
4> **NOTE**
5>
6> To use ArkTS, your DevEco Studio must be V3.0.0.900 Beta3 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. On the project configuration page, set **Compile SDK** to **9**, **Model** to **Stage**, and retain the default values for other parameters.
18
19   ![chooseStageModel](figures/chooseStageModel.png)
20
21   > **NOTE**
22   >
23   > 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
314. 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.
32
33
34## ArkTS Project Directory Structure (Stage Model)
35
36![en-us_image_0000001364054489](figures/en-us_image_0000001364054489.png)
37
38- **AppScope** > **app.json5**: global configuration of your application.
39- **entry**: OpenHarmony project module, which can be built into an OpenHarmony Ability Package ([HAP](../../glossary.md#hap)).
40- **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).
41  - **src > main > ets**: a collection of ArkTS source code.
42  - **src > main > ets > entryability**: entry to your application/service.
43  - **src > main > ets > pages**: pages included in your application/service.
44  - **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).
45  - **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).
46  - **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.
47- **hvigorfile.ts**: module-level build script. You can customize related tasks and code implementation.
48- **build-profile.json5**: application-level configuration information, including the signature and product configuration.
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** > **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**. 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
128      ![secondPage](figures/secondPage.png)
129
130      > **NOTE**
131      >
132      > 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.
133   - 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:
134
135      ```json
136      {
137        "src": [
138          "pages/Index",
139          "pages/Second"
140        ]
141      }
142      ```
143
1442. Add **\<Text>** and **\<Button>** components.
145
146   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:
147
148   ```ts
149   // Second.ets
150   @Entry
151   @Component
152   struct Second {
153     @State message: string = 'Hi there'
154
155     build() {
156       Row() {
157         Column() {
158           Text(this.message)
159             .fontSize(50)
160             .fontWeight(FontWeight.Bold)
161           Button() {
162             Text('Back')
163               .fontSize(25)
164               .fontWeight(FontWeight.Bold)
165           }
166           .type(ButtonType.Capsule)
167           .margin({
168             top: 20
169           })
170           .backgroundColor('#0D9FFB')
171           .width('40%')
172           .height('5%')
173         }
174         .width('100%')
175       }
176       .height('100%')
177     }
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.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:
189
190   ```ts
191   // Index.ets
192   // Import the router module.
193   import router from '@ohos.router';
194
195   @Entry
196   @Component
197   struct Index {
198     @State message: string = 'Hello World'
199
200     build() {
201       Row() {
202         Column() {
203           Text(this.message)
204             .fontSize(50)
205             .fontWeight(FontWeight.Bold)
206           // Add a button to respond to user clicks.
207           Button() {
208             Text('Next')
209               .fontSize(30)
210               .fontWeight(FontWeight.Bold)
211           }
212           .type(ButtonType.Capsule)
213           .margin({
214             top: 20
215           })
216           .backgroundColor('#0D9FFB')
217           .width('40%')
218           .height('5%')
219           // Bind the onClick event to the Next button so that clicking the button redirects the user to the second page.
220           .onClick(() => {
221             router.pushUrl({ url: 'pages/Second' })
222           })
223         }
224         .width('100%')
225       }
226       .height('100%')
227     }
228   }
229   ```
230
2312. Implement redirection from the second page to the first page.
232
233   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:
234
235   ```ts
236   // Second.ets
237   // Import the router module.
238   import router from '@ohos.router';
239
240   @Entry
241   @Component
242   struct Second {
243     @State message: string = 'Hi there'
244
245     build() {
246       Row() {
247         Column() {
248           Text(this.message)
249             .fontSize(50)
250             .fontWeight(FontWeight.Bold)
251           Button() {
252             Text('Back')
253               .fontSize(25)
254               .fontWeight(FontWeight.Bold)
255           }
256           .type(ButtonType.Capsule)
257           .margin({
258             top: 20
259           })
260           .backgroundColor('#0D9FFB')
261           .width('40%')
262           .height('5%')
263           // Bind the onClick event to the Back button so that clicking the button redirects the user back to the first page.
264           .onClick(() => {
265             router.back()
266           })
267         }
268         .width('100%')
269       }
270       .height('100%')
271     }
272   }
273   ```
274
2753. 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.
276
277   ![en-us_image_0000001364254729](figures/en-us_image_0000001364254729.png)
278
279
280## Running the Application on a Real Device
281
2821. Connect the development board running the OpenHarmony standard system to the computer.
283
2842. 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.
285
286   ![signConfig](figures/signConfig.png)
287
2883. 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.
289
290   ![en-us_image_0000001364254729](figures/en-us_image_0000001364254729.png)
291
292Congratulations! 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).
293