js-apis-eventhub.md 3.6 KB
Newer Older
ahjxliubao2's avatar
ahjxliubao2 已提交
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
# EventHub

- [使用说明](#使用说明)
- [on](#on)
- [off](#off)
- [emit](#emit)


> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 本模块首批接口从API 9开始支持。


事件中心。提供订阅、取消订阅、触发事件能力。


## 使用说明


​在使用eventHub的功能前,需要通过Ability实例的成员变量context获取。


  
```
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;

订阅指定事件。

ahjxliubao2's avatar
ahjxliubao2 已提交
39 40 41 42
**系统能力:**

SystemCapability.Ability.AbilityRuntime.Core

ahjxliubao2's avatar
ahjxliubao2 已提交
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
- 参数:
    | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
  | event | string | 是 | 事件名称。 | 
  | callback | Function | 是 | 事件回调,事件触发后运行。 | 

- 示例:
    
  ```
  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");
          });
          // 结果:
          // 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;

取消订阅指定事件。当callback传值时,取消订阅指定的callback;未传值时,取消订阅该事件下所有callback。

ahjxliubao2's avatar
ahjxliubao2 已提交
78 79 80 81
**系统能力:**

SystemCapability.Ability.AbilityRuntime.Core

ahjxliubao2's avatar
ahjxliubao2 已提交
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 115 116
- 参数:
    | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
  | event | string | 是 | 事件名称。 | 
  | callback | Function | 否 | 事件回调。如果不传callback,则取消订阅该事件下所有callback。 | 

- 示例:
    
  ```
  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); //取消订阅func1
          this.context.eventHub.on("123", this.func1);
          this.context.eventHub.on("123", this.func2);
          this.context.eventHub.off("123");  //取消订阅func1和func2
      }
      func1() {
          console.log("func1 is called");
      }
      func2() {
          console.log("func2 is called");
      }
  }
  ```


## emit

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

触发指定事件。

ahjxliubao2's avatar
ahjxliubao2 已提交
117 118 119 120
**系统能力:**

SystemCapability.Ability.AbilityRuntime.Core

ahjxliubao2's avatar
ahjxliubao2 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
- 参数:
    | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
  | event | string | 是 | 事件名称。 | 
  | ...args | Object[] | 是 | 可变参数,事件触发时,传递给回调函数的参数。 | 

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