鸿蒙app开发·跨模块跳转
①router:
a、使用“包名/绝对路径”进行跳转。
//需要跳转的页面:'@bundle:包名(bundleName)/模块名(moduleName)/路径(从ets开始)/页面所在的文件名(不加.ets后缀)'
router.pushUrl({
url: '@bundle:com.example.dome20250425/homePage/ets/utils/HomePage'
})
b、在目标模块页面的Entry设置routeName名字,添加依赖并引入后,使用router.pushNamedRoute()进行跳转。
//1、目标页面的Entry设置routeName
@Entry({routeName:'homePage'})
@Component
struct HomePage {
。。。。。
//2、所使用的模块'oh-package.json5'文件中添加目标模块依赖
"dependencies": {
"homepage": "file:../../dome20250425/feature/homepage"
}
//3、使用import在使用的页面中导入目标页面
import 'homepage/src/main/ets/utils/HomePage'
//4、使用pushNamedRoute进行跳转
router.pushNamedRoute({
name: 'homePage',
})
②navigation:
a、hap包跳转到hsp,hsp子模块与hsp子模块之间跳转
//1、需要跳转的页面使用后代装饰器声明stak用于控制跳转,并在最外层包裹Navigation并绑定stak
@Provide('stak') stak:NavPathStack = new NavPathStack()
。。。
Navigation(this.stak){
}
//2、需要跳转的页面通过事件控制跳转(参数:目标页面的名字,参数,跳转动画)
.onClick(() => {this.stak.pushPathByName('HomePage',null,true)})
//3、本模块引入目标模块(hsp包)的依赖("模块名":"file:...路径/模块名")
"dependencies": {
"homepage": "file:../../MyApplicationfddfgq1/feature/homepage"
}
//4、在resources/base/profile目录下新建json文件(页面名字+目标页面路径+目标页面的构建函数+(可不传但必须))
{
"routerMap": [
{
"name": "HomePage",
"pageSourceFile": "src/main/ets/utils/HomePage.ets",
"buildFunction": "HomePageBuilder",
"data": {
"description" : "this is ProductList"
}
},
{
"name": "homePage1",
"pageSourceFile": "src/main/ets/utils/homePage1.ets",
"buildFunction": "HomePageBuilder1",
"data": {
"description" : "this is ProductList"
}
}
]
}
//5、在目标页面的module.json5文件中引入上面配置的json路由文件
"module": {
"routerMap": "$profile:route_map",
}
//6、被跳转目标页面
@Component
//页面名字
struct HomePage {
@State message: string = 'HomePage 123';
//后代继承的stak
@Consume('stak') stak: NavPathStack
build() {
//子容器NavDestination
NavDestination() {
Text(this.message)
//利用名字跳转到新子页面
.onClick(() => {
this.stak.pushPathByName('homePage1',null,null)
})
}
.width('100%')
.height('100%')
}
}
//本页面的构建函数,并包裹子组件本身
@Builder
function HomePageBuilder() {
//子组件本身
HomePage()
}
//注意
具体使用即参数接收发送可参考文章:https://www.cnblogs.com/wkin/p/18815563
浙公网安备 33010602011771号