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

运行效果图如下:

相关推荐
前端不太难17 小时前
从点击到意图:鸿蒙 App 的 AI 进化
人工智能·状态模式·harmonyos
想你依然心痛17 小时前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与HMAF的“直播智脑“——PC端AI智能体电商直播中控台
人工智能·华为·harmonyos
枫叶丹417 小时前
【HarmonyOS 6.0】Enterprise Data Guard Kit:新增获取重置锁屏密码的企业恢复密钥能力详解
开发语言·华为·harmonyos
想你依然心痛17 小时前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与HMAF的“鸿蒙代码导师“——PC端AI智能体沉浸式编程学习系统
人工智能·学习·harmonyos
音视频牛哥18 小时前
SmartMediaKit 面向企业级场景的低延迟实时音视频解决方案
华为·实时音视频·harmonyos·rtsp服务器·超低延迟rtsp播放器·超低延迟rtmp播放器·实时音视频sdk
小成Coder18 小时前
【Jack实战】如何用 Core Vision Kit 给旅行票据做端侧 OCR 识别
华为·ocr·harmonyos·鸿蒙
枫叶丹418 小时前
【HarmonyOS 6.0】Device Security Kit 病毒防护服务管理能力解析
华为·harmonyos
木斯佳21 小时前
HarmonyOS 6 ArkGraphics 3D精讲:从旋转立方体看鸿蒙原生3D能力
3d·华为·harmonyos
nashane1 天前
HarmonyOS 6学习:PC端悬浮窗模式与智能长截图的协同优化实战
学习·华为·harmonyos