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%')
  }
}

运行效果图如下:

相关推荐
less_1213839 分钟前
HarmonyOS WPS Open SDK:统一版二开接入主线与能力分层概述
华为·harmonyos·wps
大雷神1 小时前
HarmonyOS APP<<古今职鉴定>>开源教程第13篇:隔空投送:ShareKit 分享服务
华为·华为云·harmonyos
时光慢煮1 小时前
开源鸿蒙PC上手体验:从 Windows 和 macOS 切换过来,我被惊艳到了
windows·开源·harmonyos
tsqtsqtsq03091 小时前
鸿蒙电脑升级 HarmonyOS 7:深度解析与 API 26 全特性指南
华为·电脑·harmonyos
lilian2334 小时前
HarmonyOS 7 新特性(一)|API 26 Beta2 十项变化与应用适配路线
华为·harmonyos
HwJack204 小时前
鸿蒙ArkData键值型数据库实战:Schema 定义与商品库存同步案例
数据库·华为·harmonyos
m0_7496902318 小时前
【寻迹校园 HarmonyOS NEXT 实战 41】用户照片、演示图与图标兜底:ReportMedia 的三层媒体策略
华为·harmonyos·arkts·媒体·隐私保护
夜雨声烦丿20 小时前
用 ArkTS 做好智能便签应用(端云协同):从核心 API 到可验证交互
华为·harmonyos
less_1213821 小时前
HarmonyOS WPS Open SDK:二开能力周回顾与联调地图
华为·harmonyos·wps
小白酷爱学习1 天前
鸿蒙OS的开发语言与工具链:如何驾驭全新开发生态!
分布式·华为·架构·harmonyos