• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.OpenLinkOptions (openLink的可选参数)
2
3<!--Kit: Ability Kit-->
4<!--Subsystem: Ability-->
5<!--Owner: @hanchen45; @Luobniz21-->
6<!--Designer: @ccllee1-->
7<!--Tester: @lixueqing513-->
8<!--Adviser: @huipeizi-->
9
10OpenLinkOptions可以作为[openLink()](js-apis-inner-application-uiAbilityContext.md#openlink12)的入参,用于标识是否仅打开AppLinking和传递键值对可选参数。
11
12> **说明:**
13>
14> - 本模块首批接口从API version 12 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
15>
16> - 本模块接口仅可在Stage模型下使用。
17
18## 导入模块
19
20```ts
21import { OpenLinkOptions } from '@kit.AbilityKit';
22```
23
24## OpenLinkOptions
25
26**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
27
28**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
29
30| 名称 | 类型 | 只读 | 可选 | 说明 |
31| -------- | -------- | -------- | -------- | -------- |
32| appLinkingOnly | boolean | 否 | 是 | 表示是否必须以<!--RP1-->[AppLinking](../../application-models/app-linking-startup.md)<!--RP1End-->的方式启动UIAbility。<br />- 取值为true时,如果不存在与AppLinking相匹配的UIAbility,直接返回。<br />- 取值为false时,如果不存在与AppLinking相匹配的UIAbility,AppLinking会退化为[DeepLinking](../../application-models/deep-linking-startup.md)。默认值为false。<br />aa命令隐式拉起Ability时可以通过设置"--pb appLinkingOnly true/false"以AppLinking的方式进行启动。 |
33| parameters | Record\<string, Object> | 否 | 是 | 表示WantParams参数。<br/>**说明**:具体使用规则请参考[want](./js-apis-app-ability-want.md)中的parameters属性。 |
34
35**示例:**
36
37  ```ts
38  import { common, OpenLinkOptions, wantConstant } from '@kit.AbilityKit';
39  import { hilog } from '@kit.PerformanceAnalysisKit';
40  import { BusinessError } from '@kit.BasicServicesKit';
41
42  const DOMAIN = 0xeeee;
43  const TAG: string = '[openLinkDemo]';
44
45  @Entry
46  @Component
47  struct Index {
48    @State message: string = 'I am caller';
49
50    build() {
51      Row() {
52        Column() {
53          Text(this.message)
54            .fontSize(50)
55            .fontWeight(FontWeight.Bold)
56          Button('start browser', { type: ButtonType.Capsule, stateEffect: true })
57            .width('87%')
58            .height('5%')
59            .margin({ bottom: '12vp' })
60            .onClick(() => {
61              let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
62              let link: string = 'https://www.example.com';
63              let openLinkOptions: OpenLinkOptions = {
64                appLinkingOnly: true,
65                parameters: {
66                  [wantConstant.Params.CONTENT_TITLE_KEY]: 'contentTitle',
67                  keyString: 'str',
68                  keyNumber: 200,
69                  keyBool: false,
70                  keyObj: {
71                    keyObjKey: 'objValue',
72                  }
73                }
74              };
75              try {
76                context.openLink(
77                  link,
78                  openLinkOptions,
79                  (err, result) => {
80                    hilog.error(DOMAIN, TAG, `openLink callback error.code: ${JSON.stringify(err)}`);
81                    hilog.info(DOMAIN, TAG, `openLink callback result: ${JSON.stringify(result.resultCode)}`);
82                    hilog.info(DOMAIN, TAG, `openLink callback result data: ${JSON.stringify(result.want)}`);
83                  }
84                ).then(() => {
85                  hilog.info(DOMAIN, TAG, `open link success.`);
86                }).catch((err: BusinessError) => {
87                  hilog.error(DOMAIN, TAG, `open link failed, errCode: ${JSON.stringify(err.code)}`);
88                });
89              }
90              catch (e) {
91                hilog.error(DOMAIN, TAG, `open link failed, errCode: ${JSON.stringify(e.code)}`);
92              }
93            })
94        }
95        .width('100%')
96      }
97      .height('100%')
98    }
99  }
100  ```
101