js-apis-eventhub.md 3.8 KB
Newer Older
W
wusongqing 已提交
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
# EventHub

> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.


Implements event subscription, unsubscription, and triggering.


## Usage


Before using any methods in the **EventHub**, you must obtain an **EventHub** instance through the member variable **context** of the **Ability** instance.


  
```
import Ability from '@ohos.application.Ability'
export default class MainAbility extends Ability {
    onForeground() {
        this.context.eventHub.on("123", this.func1);
    }
}
```


## on

on(event: string, callback: Function): void;

Subscribes to an event.

W
wusongqing 已提交
33
**System capability**:
W
wusongqing 已提交
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

SystemCapability.Ability.AbilityRuntime.Core

- Parameters
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | event | string | Yes| Event name.| 
  | callback | Function | Yes| Callback invoked when the event is triggered.| 

- Example
    
  ```
  import Ability from '@ohos.application.Ability'
  
  export default class MainAbility extends Ability {
      onForeground() {
          this.context.eventHub.on("123", this.func1);
          this.context.eventHub.on("123", () => {
              console.log("call anonymous func 1");
          });
          // Result
          // func1 is called
          // call anonymous func 1
          this.context.eventHub.emit("123"); 
      }
      func1() {
          console.log("func1 is called");
      }
  }
  ```


## off

off(event: string, callback?: Function): void;

Unsubscribes from an event. If **callback** is specified, this method unsubscribes from the specified callback. If **callback** is not specified, this method unsubscribes from all callbacks in the event.

W
wusongqing 已提交
72
**System capability**:
W
wusongqing 已提交
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

SystemCapability.Ability.AbilityRuntime.Core

- Parameters
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | event | string | Yes| Event name.| 
  | callback | Function | No| Callback for the event. If **callback** is unspecified, all callbacks of the event are unsubscribed.| 

- Example
    
  ```
  import Ability from '@ohos.application.Ability'
  
  export default class MainAbility extends Ability {
      onForeground() {
          this.context.eventHub.on("123", this.func1);
          this.context.eventHub.off("123", this.func1); // Unsubscribe from func1.
          this.context.eventHub.on("123", this.func1);
          this.context.eventHub.on("123", this.func2);
          this.context.eventHub.off("123"); // Unsubscribe from func1 and func2.
      }
      func1() {
          console.log("func1 is called");
      }
      func2() {
          console.log("func2 is called");
      }
  }
  ```


## emit

emit(event: string, ...args: Object[]): void;

Triggers an event.

W
wusongqing 已提交
111
**System capability**:
W
wusongqing 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

SystemCapability.Ability.AbilityRuntime.Core

- Parameters
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | event | string | Yes| Event name.| 
  | ...args | Object[] | Yes| Variable parameters, which are passed to the callback when the event is triggered.| 

- Example
    
  ```
  import Ability from '@ohos.application.Ability'
  
  export default class MainAbility extends Ability {
      onForeground() {
          this.context.eventHub.on("123", this.func1);
          // Result
          // func1 is called,undefined,undefined
          this.context.eventHub.emit("123");
          // Result
          // func1 is called,1,undefined
          this.context.eventHub.emit("123", 1);
          // Result
          // func1 is called,1,2
          this.context.eventHub.emit("123", 1, 2);
      }
      func1(a: string, b: string) {
          console.log("func1 is called," + a + "," + b);
      }
  }
  ```