ts-container-ability-component.md 3.8 KB
Newer Older
E
ester.zhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
# AbilityComponent

>  **NOTE**
>
>  This component is supported since API version 9.
>
>  The APIs of this module are system APIs and cannot be called by third-party applications.


**\<AbilityComponent>** is a container for independently displaying an ability.

## Constraints

Using **\<AbilityComponent>** requires a signature and related permissions.

**\<AbilityComponent>** is rendered independently and cannot be overlaid with other display content.

**\<AbilityComponent>** cannot process input events. Events are directly distributed to the internal ability for processing without passing through the current ability.

Only width and height can be set for **\<AbilityComponent>**. These attributes are mandatory and cannot be dynamically updated.

The ability to be started must be resizeable.

The ability to start must be set not to display on the recent tasks screen.

## Required Permissions

ohos.permission.INFUSE_EVENTS

ohos.permission.CONTROL_ABILITY_STACKS

ohos.permission.INTEGRATED_ABILITY

ohos.permission.INTEGRATED_INTERIOR_WINDOW


## Child Components

Not supported


## APIs

AbilityComponent(value: {want : Want, controller? : AbilityController})

- Parameters
  | Name| Type| Mandatory| Default Value| Description|
  | -------- | -------- | -------- | -------- | -------- |
  | want | [Want](../../reference/apis/js-apis-application-Want.md) | Yes| - | Description of the default ability to load.|
  | controller | [AbilityController](#abilityController) | No| - | Ability controller.|


## Events

| Name| Description|
| -------- | -------- |
| onReady()&nbsp;=&gt;&nbsp;void | Called when this **\<AbilityComponent>** is started. You can then use APIs in the **\<AbilityComponent>**.|
| onDestroy()&nbsp;=&gt;&nbsp;void | Called when this **\<AbilityComponent>** is destroyed.|
| onAbilityCreated(name:&nbsp;string)&nbsp;=&gt;&nbsp;void | Called when the specified ability is loaded.|
| onAbilityMoveToFont()&nbsp;=&gt;&nbsp;void               | Called when this ability is moved to the foreground.|
| onAbilityWillRemove()&nbsp;=&gt;&nbsp;void | Called when this ability is about to be removed.|

## AbilityController

Implements an ability controller, which provides the control APIs for **\<AbilityComponent>**.

| Name                                   | Description                                                    |
| --------------------------------------- | ------------------------------------------------------------ |
| startAbility()&nbsp;=&gt;&nbsp;want     | Loads an ability inside the **\<AbilityComponent>**.<br>**want**: description of the ability to be loaded.|
| preformBackPress()&nbsp;=&gt;&nbsp;void | Performs a return operation inside the **\<AbilityComponent>**.                        |
| getStackCount()&nbsp;=&gt;&nbsp;void    | Obtains the number of tasks in the internal task stack of the **\<AbilityComponent>**.                |


## Example

```ts
// xxx.ets
@Entry
@Component
struct MyComponent {
 @State controller: AbilityController = new AbilityController()

  build() {
      Column() {
          AbilityComponent({
              want: {
                  bundleName: '',
                  abilityName: ''
              },
              controller: this.controller
          })
          .onReady(() => {
              console.log('AbilityComponent ready');
          })
          .onDestory(() => {
              console.log('AbilityComponent destory');
          })
          Button("Start New")
          .onClick(() => {
              this.controller.startAbility({
                  bundleName: '',
                  abilityName: ''
              });
          })
          Button("Back")
          .onClick(() => {
              if (this.controller.getStacjCount() > 1) {
                  this.controller.preformBackPress();
              }
          })
      }
  }
}
```