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

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

HarmonyOS
2024-10-31 11:18:12
5860浏览
收藏 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%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
分享
微博
QQ
微信
回复
2024-10-31 16:31:45


相关问题
HarmonyOS 针对API12:组件使用场景dialog
1063浏览 • 1回复 待解决
HarmonyOS 视频场景如何实现
597浏览 • 1回复 待解决
请问ArkTS中this使用场景是什么?
2803浏览 • 1回复 待解决
HarmonyOS 请问ArkTS如何实现RSA加密?
1331浏览 • 1回复 待解决
如何通过卡片点击实现业务登录场景
2240浏览 • 1回复 待解决
如何针对不同设备场景进行调优?
266浏览 • 0回复 待解决
请问如何实现异地分布式组网?
8731浏览 • 1回复 待解决
请问多HAP包的应用场景是怎么样的?
1119浏览 • 1回复 待解决
HarmonyOS 请问ArkTS如何实现倒计时功能?
34173浏览 • 8回复 待解决
针对谷歌限制,如何自己装谷歌框架
5636浏览 • 1回复 待解决
关于HarmonyOS包大小的描述
1019浏览 • 1回复 待解决
HarmonyOS Image CAPI模块文档描述
597浏览 • 1回复 待解决
如何对UI描述进行单元测试?
1325浏览 • 1回复 待解决
使用Grid 组件实现选座场景
2159浏览 • 1回复 待解决
瀑布流场景的推荐实现方案
2771浏览 • 1回复 待解决