DEV Community

Cover image for Implementing Page Navigation
liu yang
liu yang

Posted on

Implementing Page Navigation

Implementing Page Navigation

Page - to - Page Navigation Using Router

Page navigation can be achieved through page routing using the router. The router locates the target page based on the page URL, enabling the transition. To use page routing, import the router module.

For enhanced transition effects, it is recommended to use Navigation.

Navigating from the First Page to the Second Page

In the first page, bind the navigation button to an onClick event. When the button is clicked, it will navigate to the second page. Here's an example of the Index.ets file:

// Index.ets
// Import the page routing module
import { router } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        // Add a button to respond to user clicks
        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 navigation button to navigate to the second page when clicked
        .onClick(() => {
          console.info(`Succeeded in clicking the 'Next' button.`)
          // Navigate to the second page
          router.pushUrl({ url: 'pages/Second' }).then(() => {
            console.info('Succeeded in jumping to the second page.')

          }).catch((err: BusinessError) => {
            console.error(`Failed to jump to the second page. Code is ${err.code}, message is ${err.message}`)
          })
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}
Enter fullscreen mode Exit fullscreen mode

Returning from the Second Page to the First Page

In the second page, bind the back button to an onClick event. When the button is clicked, it will return to the first page. Here's an example of the Second.ets file:

// Second.ets
// Import the page routing module
import { router } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Second {
  @State message: string = 'Hi there'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button() {
          Text('Back')
            .fontSize(30)
            .fontWeight(FontWeight.Bold)
        }
        .type(ButtonType.Capsule)
        .margin({
          top: 20
        })
        .backgroundColor('#0D9FFB')
        .width('40%')
        .height('5%')
        // Bind the onClick event to the back button to return to the first page when clicked
        .onClick(() => {
          console.info(`Succeeded in clicking the 'Back' button.`)
          try {
            // Return to the first page
            router.back()
            console.info('Succeeded in returning to the first page.')
          } catch (err) {
            let code = (err as BusinessError).code; 
            let message = (err as BusinessError).message; 
            console.error(`Failed to return to the first page. Code is ${code}, message is ${message}`)
          }
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}
Enter fullscreen mode Exit fullscreen mode

Open the Index.ets file and click the button in the previewer to refresh. The effect is shown in the figure below:

Image description

Top comments (0)