• 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**System capability**: SystemCapability.Ability.AbilityRuntime.Core
6
7| Name          | Type      | Mandatory  | Description                       |
8| ------------ | -------- | ---- | ------------------------- |
9| onConnect<sup>7+</sup>    | function | Yes   | Callback invoked when a connection is set up.     |
10| onDisconnect<sup>7+</sup> | function | Yes   | Callback invoked when a connection is interrupted.          |
11| onFailed<sup>7+</sup>     | function | Yes   | Callback invoked when a connection fails.|
12
13**Example**
14
15  ```ts
16  let want = {
17    bundleName: 'com.example.myapp',
18    abilityName: 'MyAbility'
19  };
20
21  let connectOptions = {
22    onConnect(elementName, remote) {
23        console.log('onConnect elementName: ' + elementName);
24    },
25    onDisconnect(elementName) {
26        console.log('onDisconnect elementName: ' + elementName);
27    },
28    onFailed(code) {
29        console.error('onFailed code: ' + code);
30    }
31  };
32
33  let connection = this.context.connectAbility(want, connectOptions);
34  ```
35