• 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 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. On the project configuration page, set **Compile SDK** to **9**, **Model** to **Stage**, and retain the default values for other parameters.
18
19   ![07](figures/07.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
31
32## ArkTS Project Directory Structure (Stage Model)
33
34![en-us_image_0000001364054489](figures/en-us_image_0000001364054489.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 > entryability**: entry to your application/service.
39  - **src > main > ets > pages**: pages included in your application/service.
40  - **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).
41  - **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).
42  - **build-profile.json5**: current module information and build configuration options, including **buildOption** and **targets**.
43  - **hvigorfile.ts**: module-level build script. You can customize related tasks and code implementation.
44
45- **build-profile.json5**: application-level configuration information, including the signature and product configuration.
46
47- **hvigorfile.ts**: application-level build script.
48
49
50## Building the First Page
51
521. Use the **\<Text>** component.
53
54   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:
55
56   ```ts
57   // Index.ets
58   @Entry
59   @Component
60   struct Index {
61     @State message: string = 'Hello World'
62
63     build() {
64       Row() {
65         Column() {
66           Text(this.message)
67             .fontSize(50)
68             .fontWeight(FontWeight.Bold)
69         }
70         .width('100%')
71       }
72       .height('100%')
73     }
74   }
75   ```
76
772. Add a **\<Button>** component.
78
79   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:
80
81   ```ts
82   // Index.ets
83   @Entry
84   @Component
85   struct Index {
86     @State message: string = 'Hello World'
87
88     build() {
89       Row() {
90         Column() {
91           Text(this.message)
92             .fontSize(50)
93             .fontWeight(FontWeight.Bold)
94           // Add a button to respond to user clicks.
95           Button() {
96             Text('Next')
97               .fontSize(30)
98               .fontWeight(FontWeight.Bold)
99           }
100           .type(ButtonType.Capsule)
101           .margin({
102             top: 20
103           })
104           .backgroundColor('#0D9FFB')
105           .width('40%')
106           .height('5%')
107         }
108         .width('100%')
109       }
110       .height('100%')
111     }
112   }
113   ```
114
1153. On the toolbar in the upper right corner of the editing window, click **Previewer**. Below is how the first page looks in the Previewer.
116
117   ![en-us_image_0000001311334976](figures/en-us_image_0000001311334976.png)
118
119
120## Building the Second Page
121
1221. Create the second page.
123
124   - 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.
125
126      ![09](figures/09.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: 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:
132
133      ```json
134      {
135        "src": [
136          "pages/Index",
137          "pages/Second"
138        ]
139      }
140      ```
141
1422. Add **\<Text>** and **\<Button>** components.
143
144   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:
145
146   ```ts
147   // Second.ets
148   @Entry
149   @Component
150   struct Second {
151     @State message: string = 'Hi there'
152
153     build() {
154       Row() {
155         Column() {
156           Text(this.message)
157             .fontSize(50)
158             .fontWeight(FontWeight.Bold)
159           Button() {
160             Text('Back')
161               .fontSize(25)
162               .fontWeight(FontWeight.Bold)
163           }
164           .type(ButtonType.Capsule)
165           .margin({
166             top: 20
167           })
168           .backgroundColor('#0D9FFB')
169           .width('40%')
170           .height('5%')
171         }
172         .width('100%')
173       }
174       .height('100%')
175     }
176   }
177   ```
178
179
180## Implementing Page Redirection
181
182You 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:
183
1841. Implement redirection from the first page to the second page.
185
186   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:
187
188   ```ts
189   // Index.ets
190   // Import the router module.
191   import router from '@ohos.router';
192
193   @Entry
194   @Component
195   struct Index {
196     @State message: string = 'Hello World'
197
198     build() {
199       Row() {
200         Column() {
201           Text(this.message)
202             .fontSize(50)
203             .fontWeight(FontWeight.Bold)
204           // Add a button to respond to user clicks.
205           Button() {
206             Text('Next')
207               .fontSize(30)
208               .fontWeight(FontWeight.Bold)
209           }
210           .type(ButtonType.Capsule)
211           .margin({
212             top: 20
213           })
214           .backgroundColor('#0D9FFB')
215           .width('40%')
216           .height('5%')
217           // Bind the onClick event to the Next button so that clicking the button redirects the user to the second page.
218           .onClick(() => {
219             router.pushUrl({ url: 'pages/Second' })
220           })
221         }
222         .width('100%')
223       }
224       .height('100%')
225     }
226   }
227   ```
228
2292. Implement redirection from the second page to the first page.
230
231   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:
232
233   ```ts
234   // Second.ets
235   // Import the router module.
236   import router from '@ohos.router';
237
238   @Entry
239   @Component
240   struct Second {
241     @State message: string = 'Hi there'
242
243     build() {
244       Row() {
245         Column() {
246           Text(this.message)
247             .fontSize(50)
248             .fontWeight(FontWeight.Bold)
249           Button() {
250             Text('Back')
251               .fontSize(25)
252               .fontWeight(FontWeight.Bold)
253           }
254           .type(ButtonType.Capsule)
255           .margin({
256             top: 20
257           })
258           .backgroundColor('#0D9FFB')
259           .width('40%')
260           .height('5%')
261           // Bind the onClick event to the Back button so that clicking the button redirects the user back to the first page.
262           .onClick(() => {
263             router.back()
264           })
265         }
266         .width('100%')
267       }
268       .height('100%')
269     }
270   }
271   ```
272
2733. 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.
274
275   ![en-us_image_0000001364254729](figures/en-us_image_0000001364254729.png)
276
277
278## Running the Application on a Real Device
279
2801. Connect the development board running the OpenHarmony standard system to the computer.
281
2822. 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.
283
284   ![06](figures/06.png)
285
2863. 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.
287
288   ![en-us_image_0000001364254729](figures/en-us_image_0000001364254729.png)
289
290Congratulations! 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).
291