HarmonyOS UIAbilityContext.openLink 为 undefined?

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

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

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

阅读 546
1 个回答

参考以下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%')
  }
}
logo
HarmonyOS
子站问答
访问
宣传栏