HarmonyOS路由的使用

200 阅读1分钟
  1. 导入router模块 import router from '@ohos.router'
  2. 使用router中的方法进行页面跳转,router.push方法从API version9开始不再维护,建议使用router.pushUrl
import router from '@ohos.router';

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

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

        Button('Next')
          .width(100)
          .height(50)
          .fontSize(25)
          .fontWeight(FontWeight.Bolder)
          .fontColor(Color.Blue)
          .onClick( () => {
            router.pushUrl( {url: 'pages/FatherPage'})
      })
      }
      .width('100%')
    }
    .height('100%')
  }
}
  1. 再倒入url路径时要注意路径不要写错。例如:
// 错误写法回报 03-24 23:37:58.634 18900-16500 E C03900/Ace: [manifest_router.cpp(GetPagePath)-(0)] [Engine Log] can't find this page
03-24 23:37:58.634 18900-16500 E C03900/Ace: [page_router_manager.cpp(StartPush)-(0)] [Engine Log] this uri not support in route push.这样的错误

router.pushUrl( {url: '../pages/FatherPage'})或router.pushUrl( {url: '/pages/FatherPage'})

// 正确写法
router.pushUrl( {url: 'pages/FatherPage'})