HarmonyOS router.pushUrl使用基于Har

现在有 A、B两个模块是Feature类型,他们之间分别有各自的page ,请问怎么通过 router.pushUrl 进行跳转?

HarmonyOS
18h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

可以使用命名路由,demo:

Index.ets:

import { UIContext } from "@ohos.arkui.UIContext";
import { BusinessError } from '@kit.BasicServicesKit';
import router from '@ohos.router';
import('@ohos/library/src/main/ets/components/mainpage/MainPage'); // 引入共享包中的命名路由页面,要引入

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

  @StorageProp('_UIContext') _UIContext: UIContext | null = null;

  image1_onClick_router_back = () => {
    try {
      router.pushNamedRoute({
        name: 'mainPage',
        params: {
        }
      })
    } catch(err) {
      console.log(`error code is: ${(err as BusinessError).name}, error message is: ${(err as BusinessError).message}`);
    }
  }

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(this.image1_onClick_router_back)
    }
    .height('100%')
    .width('100%')
  }
}

MainPage.ets

@Entry({ routeName: 'mainPage' })
@Component
export struct MainPage {
  @State message: string = 'Hello World33333';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}

需要修改oh-package.json5,添加依赖

{
  "name": "entry",
  "version": "1.0.0",
  "description": "Please describe the basic information.",
  "main": "",
  "author": "",
  "license": "",
  "dependencies": {
  "@ohos/library": "file:../third"
  }
}

参考链接:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-routing-V5#命名路由

分享
微博
QQ
微信
回复
15h前
相关问题
router.pushUrl 无法使用Map类型参数
513浏览 • 1回复 待解决
HarmonyOS router.pushUrl跳转报100002
601浏览 • 1回复 待解决
router.pushUrl是否无法使用Map类型参数
787浏览 • 1回复 待解决
同一个HSP中,router.pushUrl中的url问题
429浏览 • 1回复 待解决
HarmonyOS router pushUrl报错
712浏览 • 1回复 待解决