HarmonyOS EntryAbility跳转页面如何携带参数?

我在服务卡片通过router方式拉起EntryAbility,并在params里面加入了所需参数,那么我该如何在EntryAbility里通过参数判断跳转到指定页面,因为指定页面放在其他module里面, windowStage.loadContent无法跳转,所以我想把参数通过 windowStage.loadContent跳转到index.page 然后在index.page通过参数再跳转到我指定的page,该如何实现?

HarmonyOS
2024-10-22 10:52:16
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

简单的demo如下:

//卡片页面的ets

@Entry()  
@Component  
struct WidgetCard {  
  /*  
   * The title.  
   */  
  readonly TITLE: string = 'Hello World';  
  /*  
   * The action type.  
   */  
  readonly ACTION_TYPE: string = 'router';  
  /*  
   * The ability name.  
   */  
  readonly ABILITY_NAME: string = 'EntryAbility';  
  /*  
   * The message.  
   */  
  readonly MESSAGE: string = 'pages/searchSwiperPage';  
  /*  
   * The width percentage setting.  
   */  
  readonly FULL_WIDTH_PERCENT: string = '100%';  
  /*  
   * The height percentage setting.  
   */  
  readonly FULL_HEIGHT_PERCENT: string = '100%';  
  
  build() {  
    FormLink({  
      action: this.ACTION_TYPE,  
      abilityName: this.ABILITY_NAME,  
      params: {  
        message: this.MESSAGE,  
        type :'widgetCard'  
      }  
    }) {  
      Row() {  
        Column() {  
          Text(this.TITLE)  
            .fontSize($r('app.float.font_size'))  
            .fontWeight(FontWeight.Medium)  
            .fontColor($r('app.color.item_title_font'))  
  
        }  
        .width(this.FULL_WIDTH_PERCENT)  
      }  
      .height(this.FULL_HEIGHT_PERCENT)  
    }  
  }  
}

//app的EntryAbility

import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';  
import { hilog } from '@kit.PerformanceAnalysisKit';  
import { UIContext, window } from '@kit.ArkUI';  
  
export default class EntryAbility extends UIAbility {  
  
  private isWidgetCard :boolean = false  
  private routerStr :string =''  
  
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {  
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');  
    this.recordedWidgetCard(want)  
  }  
  //记录卡片传入的参数,也可以用首选项记录  
  recordedWidgetCard(want:Want){  
    if (!want) {  
      this.isWidgetCard = false  
      this.routerStr = ''  
    }  
    let param =  want.parameters as Record<string, Object>;  
    let routerStr = param.message as string  
    let typeTemp = param.type as string  
    if (typeTemp === 'widgetCard') {  
      this.isWidgetCard = true  
      this.routerStr = routerStr  
  
    }  
    console.log(`recordedWidgetCard, want: ${routerStr}`);  
  }  
  
  onDestroy(): void {  
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');  
  }  
  
  onWindowStageCreate(windowStage: window.WindowStage): void {  
    // Main window is created, set main page for this ability  
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');  
    let routerStr = 'pages/Index'  
    //冷启动判断  
    if(this.isWidgetCard){  
      routerStr = this.routerStr  
    }  
    windowStage.loadContent(routerStr, (err) => {  
      if (err.code) {  
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');  
        return;  
      }  
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');  
    });  
  
  }  
  
  onWindowStageDestroy(): void {  
    // Main window is destroyed, release UI related resources  
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');  
  }  
  
  onForeground(): void {  
    // Ability has brought to foreground  
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');  
  }  
  
  onBackground(): void {  
    // Ability has back to background  
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');  
  }  
  
  //热启动,跳转到具体到页面  
  onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) {  
    console.log(`onNewWant, want: ${want.abilityName}`);  
    window.getLastWindow(getContext(this), (error, topWindow) => {  
      if (topWindow) {  
        console.log(`onNewWant, want: ${JSON.stringify(want.parameters)}`);  
        let param =  want.parameters as Record<string, Object>;  
        let routerStr = param.message as string  
        if(routerStr != undefined){  
          let typeTemp = param.type as string//设置数据来源类型,便于跳转后的处理判断  
          let uiContext = topWindow.getUIContext();  
          let router = uiContext.getRouter();  
          router.pushUrl({  
            url: routerStr,  
            params: {  
              type: typeTemp  
            }  
          })  
        }  
  
      }  
    });  
  }  
}
分享
微博
QQ
微信
回复
2024-10-22 17:52:16
相关问题
HarmonyOS 页面如何设置全屏显示?
369浏览 • 1回复 待解决
HarmonyOS 页面数据携带返回问题
378浏览 • 1回复 待解决
前端页面和原生页面如何进行通信?
352浏览 • 1回复 待解决
router.back如何携带参数返回给上一级
1537浏览 • 1回复 待解决
page页面如何设置为横屏显示
1682浏览 • 1回复 待解决
H5页面如何与ArkTS交互
3001浏览 • 1回复 待解决
HarmonyOS 推送如何携带数据?
141浏览 • 1回复 待解决
Web组件和h5页面如何交互?
215浏览 • 1回复 待解决