请问针对下面场景描述如何实现 ?

模块相互依赖:A hap有依赖两个har: B和C。har B需要调用har C提供的方法,har C也需要调用har B提供的方法。请问针对上述场景如何实现。

HarmonyOS
2024-10-31 11:18:12
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

参考以下demo:

//entry/Index.ets 
import { router } from '@kit.ArkUI'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
import { common, Want } from '@kit.AbilityKit'; 
// 要跳转har内的,必须要这么导入 
import('harLibrary/src/main/ets/pages/Index') 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
  private context = getContext(this) as common.UIAbilityContext; 
 
  aboutToAppear(): void { 
    console.log("dhsgfsdfc") 
  } 
  onPageHide(): void { 
    console.log("sjfhdusghfjsf") 
  } 
  build() { 
    Column({ space: 10}) { 
      Button('打开feature的Ability') 
        .onClick(() => { 
          let wantInfo: Want = { 
            deviceId: '', // deviceId为空表示本设备 
            bundleName: 'com.example.modulejump', 
            moduleName: 'feature', // moduleName非必选 
            abilityName: 'FeatureAbility', 
            parameters: { // 自定义信息 
              info: '来自EntryAbility Page_UIAbilityComponentsInteractive页面' 
            }, 
          } 
          // context为调用方UIAbility的UIAbilityContext 
          this.context.startAbility(wantInfo).then(() => { 
            console.info('startAbility success.'); 
          }).catch((error: BusinessError) => { 
            console.error('startAbility failed: ' + JSON.stringify(error)); 
          }); 
        }) 
      Button('跳转har内页面') 
        .onClick(() => { 
          try { 
            router.pushNamedRoute({ 
              name: 'harPage', 
              params: { 
                data1: 'message', 
                data2: { 
                  data3: [123, 456, 789] 
                } 
              } 
            }) 
          } catch (err) { 
            let message: string = (err as BusinessError).message 
            let code: number = (err as BusinessError).code 
            console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`); 
          } 
        }) 
      Button('跳转hsp内页面') 
        .onClick(() => { 
          router.pushUrl({ 
            url: '@bundle:com.example.modulejump/hspLibrary/ets/pages/Index', 
            params: { 
              data1: 'message', 
              data2: { 
                data3: [123, 456, 789] 
              } 
            } 
          }) 
        }) 
    } 
    .width('100%') 
  } 
} 
//feature/Index.ets 
import { router } from '@kit.ArkUI'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
@Entry 
@Component 
struct Index { 
  @State message: string = 'feature Hello World'; 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
 
        Button('back') 
          .onClick(()=> { 
            try { 
              router.showAlertBeforeBackPage({ 
                message: '是否确认返回上一页?' 
              }); 
            } catch (err) { 
              console.error(`showAlertBeforeBackPage failed, code is ${(err as BusinessError).code}, 
message is ${(err as BusinessError).message}`); 
            } 
            router.back() 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
//har/Index.ets 
// 要能跳转进来,需要:定义routeName + export导出 
import { router } from '@kit.ArkUI'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
 
@Entry({ routeName: 'harPage' }) 
@Component 
export struct HarPage { 
  @State message: string = 'Hello Har Page'; 
  aboutToAppear(): void { 
    console.info('HarPage params: ' + JSON.stringify(router.getParams())) 
  } 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
 
        Button('back') 
          .onClick(()=> { 
            try { 
              router.back({ 
                url:"Index", 
                params:{ 
                  data:"data" 
                } 
                //message: '是否确认返回上一页?' 
              }); 
              /*router.back({ 
              url:"one", 
              params:{ 
              data:"data" 
              } 
              //message: '是否确认返回上一页?' 
              });*/ 
            } catch (err) { 
              console.error(`showAlertBeforeBackPage failed, code is ${(err as BusinessError).code}, 
message is ${(err as BusinessError).message}`); 
            } 
            router.back() 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
//hsp/Index.ets 
import { router } from '@kit.ArkUI'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
 
@Entry 
@Component 
struct HspPage { 
  @State message: string = 'Hello Hsp Page'; 
  aboutToAppear(): void { 
    console.info('HspPage params: ' + JSON.stringify(router.getParams())) 
  } 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
 
        Button('跳转Har') 
          .onClick(() => { 
            try { 
              router.pushNamedRoute({ 
                name: 'harPage', 
                params: { 
                  data1: 'message', 
                  data2: { 
                    data3: [123, 456, 789] 
                  } 
                } 
              }) 
            } catch (err) { 
              let message: string = (err as BusinessError).message 
              let code: number = (err as BusinessError).code 
              console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`); 
            } 
          }) 
        Button('back') 
          .onClick(()=> { 
            try { 
              router.showAlertBeforeBackPage({ 
                message: '是否确认返回上一页?' 
              }); 
            } catch (err) { 
              console.error(`showAlertBeforeBackPage failed, code is ${(err as BusinessError).code}, 
message is ${(err as BusinessError).message}`); 
            } 
            router.back() 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
分享
微博
QQ
微信
回复
2024-10-31 16:31:45
相关问题
请问ArkTS中this使用场景是什么?
1983浏览 • 1回复 待解决
关于HarmonyOS包大小的描述
250浏览 • 1回复 待解决
如何对UI描述进行单元测试?
568浏览 • 1回复 待解决
HarmonyOS 请问ArkTS如何实现RSA加密?
477浏览 • 1回复 待解决
如何通过卡片点击实现业务登录场景
1738浏览 • 1回复 待解决
请问多HAP包的应用场景是怎么样的?
197浏览 • 1回复 待解决
动态申请权限能否添加描述
710浏览 • 1回复 待解决
请问如何实现异地分布式组网?
7676浏览 • 1回复 待解决
针对字节流的解码工具
1003浏览 • 1回复 待解决
针对谷歌限制,如何自己装谷歌框架
4911浏览 • 1回复 待解决
使用Grid 组件实现选座场景
1011浏览 • 1回复 待解决
瀑布流场景的推荐实现方案
1897浏览 • 1回复 待解决
napi_module结构体字段描述解析
1987浏览 • 1回复 待解决
HarmonyOS 请问ArkTS如何实现倒计时功能?
32381浏览 • 8回复 待解决
Canvas如何绘制app.media下面的图片?
2331浏览 • 1回复 待解决
视频播放黑屏,下面是代码
9514浏览 • 1回复 待解决
实名购票场景实现智能填充的方式
451浏览 • 1回复 待解决