当前位置:   article > 正文

HarmonyOS router页面跳转_harmony router

harmony router

默认启动页面index.ets

  1. import router from '@ohos.router'
  2. import {BusinessError} from '@ohos.base'
  3. @Entry
  4. @Component
  5. struct Index {
  6. @State message: string = 'Hello World';
  7. build() {
  8. Row() {
  9. Column() {
  10. Text(this.message)
  11. .fontSize(50)
  12. .fontWeight(FontWeight.Bold)
  13. //添加按钮,以响应用户点击
  14. Button(){
  15. Text('Next')
  16. .fontSize(30)
  17. .fontWeight(FontWeight.Bold)
  18. }
  19. .type(ButtonType.Capsule)
  20. .margin({
  21. top:20
  22. })
  23. .backgroundColor('#00D9FFB')
  24. .width('40%')
  25. .height('5%')
  26. //跳转按钮绑定onclick事件,点击跳转到目标页面
  27. .onClick(()=>{
  28. console.log(`Succeeded in clicking the 'Next' button.`);
  29. router.pushUrl({url:'pages/TargetPage'}).then(()=>{
  30. console.info('Succeeded in jumping to the second page.')
  31. }).catch((err: BusinessError) => {
  32. console.error(`Failed to jump to the second page.Code is ${err.code}, message is ${err.message}`)
  33. })
  34. })
  35. }
  36. .width('100%')
  37. }
  38. .height('100%')
  39. }
  40. }

跳转目标页面TargetPage.ets

  1. // 导入页面路由模块
  2. import router from '@ohos.router';
  3. import { BusinessError } from '@ohos.base';
  4. @Entry
  5. @Component
  6. struct TargetPage {
  7. @State message: string = '鸿蒙小趣';
  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({top:20})
  21. .backgroundColor('#0D9FFB')
  22. .width('40%')
  23. .height('5%')
  24. // 返回按钮绑定onClick事件,点击按钮时返回到第一页
  25. .onClick(()=>{
  26. console.info(`Succeeded in clicking the 'Back' button.`)
  27. try {
  28. // 返回第一页
  29. router.back()
  30. console.info('Succeeded in returning to the first page.')
  31. }catch (err){
  32. let code = (err as BusinessError).code;
  33. let message = (err as BusinessError).message;
  34. console.error(`Failed to return to the first page.Code is ${code}, message is ${message}`)
  35. }
  36. })
  37. }
  38. .width('100%')
  39. }
  40. .height('100%')
  41. }
  42. }

运行效果图如下:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/332781
推荐阅读
相关标签
  

闽ICP备14008679号