HarmonyOS router页面跳转

默认启动页面index.ets

import  router from '@ohos.router'
import  {BusinessError} from '@ohos.base'

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

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)

        //添加按钮,以响应用户点击
        Button(){
          Text('Next')
            .fontSize(30)
            .fontWeight(FontWeight.Bold)
        }
        .type(ButtonType.Capsule)
        .margin({
          top:20
        })
        .backgroundColor('#00D9FFB')
        .width('40%')
        .height('5%')
        //跳转按钮绑定onclick事件,点击跳转到目标页面
        .onClick(()=>{
          console.log(`Succeeded in clicking the 'Next' button.`);
          router.pushUrl({url:'pages/TargetPage'}).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%')
  }
}

跳转目标页面TargetPage.ets

// 导入页面路由模块
import router from '@ohos.router';
import { BusinessError } from '@ohos.base';

@Entry
@Component
struct TargetPage {
  @State message: string = '鸿蒙小趣';

  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%')
        // 返回按钮绑定onClick事件,点击按钮时返回到第一页
        .onClick(()=>{
          console.info(`Succeeded in clicking the 'Back' button.`)
          try {
            // 返回第一页
            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%')
  }
}

运行效果图如下:

相关推荐

  1. vue怎么页面

    2024-02-19 09:44:02       41 阅读
  2. 微信小程序:页面

    2024-02-19 09:44:02       64 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-19 09:44:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-19 09:44:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-19 09:44:02       87 阅读
  4. Python语言-面向对象

    2024-02-19 09:44:02       96 阅读

热门阅读

  1. 低代码开发:助力企业迈向智能化未来

    2024-02-19 09:44:02       59 阅读
  2. C++的虚函数和纯虚函数的功能是什么

    2024-02-19 09:44:02       53 阅读
  3. docker修改工作目录

    2024-02-19 09:44:02       56 阅读
  4. MVCC简记

    2024-02-19 09:44:02       57 阅读
  5. 【nginx实践连载-4】彻底卸载Nginx(Ubuntu)

    2024-02-19 09:44:02       54 阅读
  6. Python内置函数05——filter

    2024-02-19 09:44:02       45 阅读
  7. pytorch导出为onnx,使用onnxruntime进行推理

    2024-02-19 09:44:02       41 阅读
  8. UDP协议

    UDP协议

    2024-02-19 09:44:02      51 阅读
  9. [Optimization] Codes Answer to online quiz 1

    2024-02-19 09:44:02       49 阅读
  10. harbor v1.7.1镜像仓库无法访问,并提示502 Bad Gateway

    2024-02-19 09:44:02       48 阅读