HarmonyOS 不同hap包之间的page能否互相调用

现在在首页底部状态栏有3个按钮,希望点击每个按钮,之间的界面都切换page,但是三个page处于不同的hap包中,希望实现可分可合的理念,所以想要知道,有什么好的处理方案,能够调用到不同hap包的page界面。

HarmonyOS
2024-12-20 16:11:38
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

通过tabs和navigation实现

//entry.ets
import { FeatureAIndex } from "featureAPage/src/main/ets/pages/FeatureAIndex"
import { FeatureBIndex } from "featureBPage/src/main/ets/pages/FeatureBIndex"

@Entry
@Component
struct HapTestEntryPageTest{
  @State message: string = 'Hello World Index'
  @State entryHapRouter: NavPathStack = new NavPathStack();
  @StorageLink("PageIndex") currentPageIndex: number = 0
  //定义页面栈
  @State hapArr:string[] = ["featureA","featureB"]

  @Builder
  routerMap(builderName: string, param: object) {
    if (builderName === 'featureA') {
      FeatureIndex(new Object({name: 'featureA'}));
    }else if(builderName === 'featureB'){
      FeatureBIndex(new Object({name: 'featureB'}));
    }
  };

  aboutToAppear(): void {
    setTimeout(()=>{
      //入栈,后入先出
      for(let i = 0;i<this.hapArr.length;i++){
        let pathInfo : NavPathInfo = new NavPathInfo(this.hapArr[i], null)
        this.entryHapRouter.pushPath(pathInfo, false);
      }
      //默认展示第一页 featureA
      this.entryHapRouter.moveIndexToTop(0)
    },100)
  }

  build() {
    Tabs(
      { barPosition: BarPosition.End ,
        index:$$this.currentPageIndex }
    ){
      TabContent(){
        Navigation(
          this.entryHapRouter
        )
          .navDestination(this.routerMap)
      }
      .tabBar("hapA")

      TabContent(){
        Navigation(
          this.entryHapRouter
        )
          .navDestination(this.routerMap)
      }
      .tabBar("hapB")

    }
    .animationDuration(1)
    .barWidth("100%")
    .barHeight(56)
    .scrollable(false)
    .backgroundColor("#f1f3f5")
    .onAnimationStart(
      //将指定名称的页面推到栈顶,设置无动画
      (index: number,targetIndex:number)=>{
        this.entryHapRouter.moveToTop(this.hapArr[targetIndex],false)
      }
    )

  }

}

//entry模块的 oh-package.json5 文件配置hap包的路径 dependencies

"dependencies": {
  "featureAPage": "file:../featureA",
  "featureBPage": "file:../featureB"
}

// featureA.ets
@Builder
export function FeatureAIndex(value: object) {
  NavDestination() {
    Column() {
      Text('Hello FeatureA Page')
      Text(`传入的参数:${JSON.stringify(value)}`)
        .margin(20)
    }
    .width('100%')
    .height('100%')
  }
  .hideTitleBar(true)
}
//featureB.ets
@Builder
export function FeatureBIndex(value: object) {
  NavDestination() {
    Column() {
      Text('Hello FeatureB Page')
      Text(`传入的参数:${JSON.stringify(value)}`)
        .margin(20)
    }
    .width('100%')
    .height('100%')
  }
  .hideTitleBar(true)
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-20 18:15:07
相关问题
HarmonyOS 不同har之间怎么调用
721浏览 • 1回复 待解决
多模块之间数据互相访问
938浏览 • 1回复 待解决
HarmonyOS 多个HSP之间如何互相通讯
1162浏览 • 1回复 待解决
如何处理HAR之间互相依赖
3152浏览 • 1回复 待解决
HarmonyOS page之间相互传值
474浏览 • 1回复 待解决
HarmonyOS har是否不能互相引用
1590浏览 • 1回复 待解决
arkTs能和arkjs互相调用吗?
3013浏览 • 1回复 待解决
hsp和har互相转换方案
2289浏览 • 1回复 待解决
HarmonyOS HAP之间如何进行通信?
1212浏览 • 1回复 待解决
HarmonyOS HAR之间路由跳转
1411浏览 • 1回复 待解决
HarmonyOS Webkit ets之间调用
1166浏览 • 1回复 待解决
ArkTS与C++互相直接调用
4176浏览 • 1回复 待解决
HarmonyOS 不同页面之间如何响应通信
790浏览 • 1回复 待解决