W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

9/30/2021

Getting Started with ArkTS |HarmonyOS Tutorial

NOTE

To use ArkTS, your DevEco Studio must be V3.0.0.601 Beta1 or later.

When using the emulator, select a device that complies with API version 7 or later.

For best possible results, use DevEco Studio V3.0.0.993 for your development.


Creating an ArkTS Project
  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 HarmonyOS tab of the Choose Your Ability Template page, select Empty Ability and click Next.

    2. On the project configuration page, set Language to eTS and retain the default values for other parameters.

  2. NOTE

    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.

    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.

    To use the low-code development mode, turn on Enable Super Visual on the page shown above.

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

ArkTS Project Directory Structure

  • entry: HarmonyOS project module, which can be built into a HarmonyOS Ability Package (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, that is, the home page of your application.
    • src > main > ets > MainAbility > app.ets: ability lifecycle file.
    • 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 File Categories.
    • 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 about the configuration file, see Application Package Structure Configuration File (JavaScript/ArkTS).
    • build-profile.json5: current module information and build configuration options, including buildOption and targets.
    • hvigorfile.js: module-level compilation and build task script. You can customize related tasks and code implementation.
  • build-profile.json5: application-level configuration information, including the signature and product configuration.

  • hvigorfile.js: application-level compilation and build task script.

Building the First Page

  1. Use the <Text> component.

    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:

    1. // index.ets
    2. @Entry
    3. @Component
    4. struct Index {
    5. @State message: string = 'Hello World'
    6. build() {
    7. Row() {
    8. Column() {
    9. Text(this.message)
    10. .fontSize(50)
    11. .fontWeight(FontWeight.Bold)
    12. }
    13. .width('100%')
    14. }
    15. .height('100%')
    16. }
    17. }
  2. Add a <Button> component.

    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:

    1. // index.ets
    2. @Entry
    3. @Component
    4. struct Index {
    5. @State message: string = 'Hello World'
    6. build() {
    7. Row() {
    8. Column() {
    9. Text(this.message)
    10. .fontSize(50)
    11. .fontWeight(FontWeight.Bold)
    12. // Add a button to respond to user clicks.
    13. Button() {
    14. Text('Next')
    15. .fontSize(30)
    16. .fontWeight(FontWeight.Bold)
    17. }
    18. .type(ButtonType.Capsule)
    19. .margin({
    20. top: 20
    21. })
    22. .backgroundColor('#0D9FFB')
    23. .width('40%')
    24. .height('5%')
    25. }
    26. .width('100%')
    27. }
    28. .height('100%')
    29. }
    30. }
  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.

Building the Second Page

  1. Create the second page.
    • Create the second page file: In the Project window, choose entry > src > main > ets > MainAbility. Right-click the pages folder, choose New > eTS File, name the page second, and click Finish. Below shows the created page in the pages folder.

      NOTE

      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.

    • 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:
      1. {
      2. ...
      3. "module": {
      4. "js": [
      5. {
      6. ...
      7. "pages": [
      8. "pages/index",
      9. "pages/second"
      10. ],
      11. ...
      12. }
      13. }
      14. ]
      15. }
      16. }
  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:

    1. // second.ets
    2. @Entry
    3. @Component
    4. struct Second {
    5. @State message: string = 'Hi there'
    6. build() {
    7. Row() {
    8. Column() {
    9. Text(this.message)
    10. .fontSize(50)
    11. .fontWeight(FontWeight.Bold)
    12. Button() {
    13. Text('Back')
    14. .fontSize(25)
    15. .fontWeight(FontWeight.Bold)
    16. }
    17. .type(ButtonType.Capsule)
    18. .margin({
    19. top: 20
    20. })
    21. .backgroundColor('#0D9FFB')
    22. .width('40%')
    23. .height('5%')
    24. }
    25. .width('100%')
    26. }
    27. .height('100%')
    28. }
    29. }

Implementing Page Redirection

You can implement page redirection through the page router, which finds the target page based on the page URL. Import the router module and then perform the steps below:

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

    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:

    1. // index.ets
    2. // The @ohos.router module is supported since API version 8. Use an SDK of API version 8 or later.
    3. import router from '@ohos.router';
    4. @Entry
    5. @Component
    6. struct Index {
    7. @State message: string = 'Hello World'
    8. build() {
    9. Row() {
    10. Column() {
    11. Text(this.message)
    12. .fontSize(50)
    13. .fontWeight(FontWeight.Bold)
    14. // Add a button to respond to user clicks.
    15. Button() {
    16. Text('Next')
    17. .fontSize(30)
    18. .fontWeight(FontWeight.Bold)
    19. }
    20. .type(ButtonType.Capsule)
    21. .margin({
    22. top: 20
    23. })
    24. .backgroundColor('#0D9FFB')
    25. .width('40%')
    26. .height('5%')
    27. // Bind the onClick event to the Next button so that clicking the button redirects the user to the second page.
    28. .onClick(() => {
    29. router.push({ url: 'pages/second' })
    30. })
    31. }
    32. .width('100%')
    33. }
    34. .height('100%')
    35. }
    36. }
  2. Implement redirection from the second page to the first page.

    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:

    1. // second.ets
    2. // The @ohos.router module is supported since API version 8. Use an SDK of API version 8 or later.
    3. import router from '@ohos.router';
    4. @Entry
    5. @Component
    6. struct Second {
    7. @State message: string = 'Hi there'
    8. build() {
    9. Row() {
    10. Column() {
    11. Text(this.message)
    12. .fontSize(50)
    13. .fontWeight(FontWeight.Bold)
    14. Button() {
    15. Text('Back')
    16. .fontSize(25)
    17. .fontWeight(FontWeight.Bold)
    18. }
    19. .type(ButtonType.Capsule)
    20. .margin({
    21. top: 20
    22. })
    23. .backgroundColor('#0D9FFB')
    24. .width('40%')
    25. .height('5%')
    26. // Bind the onClick event to the Back button so that clicking the button redirects the user back to the first page.
    27. .onClick(() => {
    28. router.back()
    29. })
    30. }
    31. .width('100%')
    32. }
    33. .height('100%')
    34. }
    35. }
  3. Open the index.ets file and click  in the Previewer to refresh the file. The display effect is shown in the figure below.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.