简体中文
Event
约 206 字小于 1 分钟
2025-11-24
类 EventBus,可用于在插件内通信,所有插件共享,并在宿主生命周期内存活。
方法说明
| 方面 | 说明 | 定义 |
|---|---|---|
| onEvent | 监听事件 | onEvent=>(eventName, fn){} |
| emitEvent | 发送事件 | emitEvent=>(eventName, opt){} |
| hasEvent | 判断事件是否被监听 | hasEvent=>(eventName){} |
| findOffEvent | 取消掉指定的监听方法 | findOffEvent=>(eventName, predicate){} |
代码示例
onEvent
{
event: "form.render.end",
functional: function (pluginExecResult, options) {
console.log("custom plugin: form.render.end exec");
let {bizApi}=options;
let listener= (opt)=>{
console.log("customEvent::"opt);
}
// 监听事件
bizApi.onEvent("customEvent", listener);
//判断事件是否被监听
console.log("hasEvent::",bizApi.hasEvent("customEvent"));//expect out: hasEvent:: true
//发送事件
bizApi.emitEvent("customEvent",{test:"test 1"})//expect out: customEvent::{test:"test 1"}
//取消掉指定的监听方法
bizApi.findOffEvent("customEvent", lis=>lis===listener);
//再次发送事件
bizApi.emitEvent("customEvent",{test:"test 2"})//expect out: nothing
}
}注意
- 仅可以当前新建编辑页生命周期内互相通信
