• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ConnectOptions
2
3**ConnectOptions** can be used as an input parameter to receive status changes during the connection to a background service. For example, it is used as an input parameter of [connectServiceExtensionAbility](js-apis-inner-application-serviceExtensionContext.md#serviceextensioncontextconnectserviceextensionability) to connect to a ServiceExtensionAbility.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```ts
12import common from '@ohos.app.ability.common';
13```
14
15**System capability**: SystemCapability.Ability.AbilityRuntime.Core
16
17| Name          | Type      | Mandatory  | Description                       |
18| ------------ | -------- | ---- | ------------------------- |
19| onConnect<sup>7+</sup>    | function | Yes   | Callback invoked when a connection is set up.     |
20| onDisconnect<sup>7+</sup> | function | Yes   | Callback invoked when a connection is interrupted.          |
21| onFailed<sup>7+</sup>     | function | Yes   | Callback invoked when a connection fails.|
22
23**Example**
24
25  ```ts
26  let want = {
27    bundleName: 'com.example.myapp',
28    abilityName: 'MyAbility'
29  };
30
31  let connectOptions: common.ConnectOptions = {
32    onConnect(elementName, remote) {
33        console.log('onConnect elementName: ' + elementName);
34    },
35    onDisconnect(elementName) {
36        console.log('onDisconnect elementName: ' + elementName);
37    },
38    onFailed(code) {
39        console.error('onFailed code: ' + code);
40    }
41  };
42
43  let connection = this.context.connectAbility(want, connectOptions);
44  ```
45