首页 小组 问答 话题 好文 素材 用户 唠叨 我的社区

[分享]HarmonyOS @Entry 并不能封装到库使用 ?

不爱phpLv.1普通用户
2024-09-05 08:14:46
0
180

可以使用router.pushNamedRoute()来实现。请参考如下demo:
//做改动的第1个文件:

// 在想要跳转到的共享包页面里,给@Entry修饰的自定义组件命名: 
// library为新建共享包自定义的名字 
@Entry({ routeName: 'lib_page' }) 
@Component 
export struct MainPage{ 
  build() { 
    Row() { 
      Column() { 
        Text('Library Page') 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 

//做改动的第2个文件:

 
// 跳转页面导入路由模块 
import { router } from '@kit.ArkUI'; 
// 引入共享包中的命名路由页面 
import ('library/src/main/ets/components/mainpage/MainPage'); 
 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
        Blank(); 
        Button('Click').onClick(() => { // 点击跳转到其他共享包中的页面 
          router.pushNamedRoute({ 
            name: 'lib_page', 
            params: {} 
          }) 
        }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 

//做改动的第3个文件:

//使用命名路由方式跳转时,需要在当前应用包的oh-package.json5文件中配置依赖 
"dependencies": { 
  "library": "file:../library" 
}
不爱php
不爱php

47 天前

签名 : 智者不入爱河   180       0
评论
站长交流