HarmonyOS UIAbilityContext.openLink 为 undefined

UIAbilityContext.openLink 不可用,暂时无法使用 openLink 实现应用间跳转

1、新建空项目,在 EntryAbility 的 onCreate 回调中打断点

2、输入 this.context.openLink,结果输出 result=undefined

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

参考以下Demo,使用openLink跳转外部应用

import { common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import OpenLinkOptions from '@ohos.app.ability.OpenLinkOptions';
import { BusinessError } from '@kit.BasicServicesKit';

const DOMAIN = 0xeeee;
const TAG: string = '[openLinkDemo]';

@Entry
@Component
struct Index {
  @State message: string = 'I am caller';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button('start browser', { type: ButtonType.Capsule, stateEffect: true })
          .width('87%')
          .height('5%')
          .margin({ bottom: '12vp' })
          .onClick(() => {
            let context = getContext(this) as common.UIAbilityContext;
            let link: string = 'https://www.index.com';
            let openLinkOptions: OpenLinkOptions = {
              appLinkingOnly: true,
              parameters: { demo_key: 'demo_value' }
            };

            try {
              context.openLink(
                link,
                openLinkOptions,
                (err, result) => {
                  hilog.error(DOMAIN, TAG, `openLink callback error.code: ${JSON.stringify(err)}`);
                  hilog.info(DOMAIN, TAG, `openLink callback result: ${JSON.stringify(result.resultCode)}`);
                  hilog.info(DOMAIN, TAG, `openLink callback result data: ${JSON.stringify(result.want)}`);
                }
              ).then(() => {
                hilog.info(DOMAIN, TAG, `open link success.`);
              }).catch((err: BusinessError) => {
                hilog.error(DOMAIN, TAG, `open link failed, errCode ${JSON.stringify(err.code)}`);
              });
            }
            catch (e) {
              hilog.error(DOMAIN, TAG, `exception occured, errCode ${JSON.stringify(e.code)}`);
            }
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
HarmonyOS SourceTool.xxx值undefined
38浏览 • 1回复 待解决
如何获取单例undefined
503浏览 • 1回复 待解决
如何获取UIAbilityContext
1930浏览 • 1回复 待解决
如何在UI中获取UIAbilityContext
2582浏览 • 1回复 待解决