事件、Schema 与组件注册
前端事件、权限判断和 Schema 渲染在挂载后使用;组件、动作和校验器可在组件库入口加载时注册。
EventBus
| 方法 | 用途 |
|---|---|
subscribe(event, handler) | 持续监听 |
subscribeOnce(event, handler) | 一次性监听 |
unsubscribe(event, handler) | 移除指定处理函数 |
unsubscribeAll(event) | 移除该事件的所有监听 |
publish(event, data, options?) | 发布事件 |
import { EventBus, initMicroApp } from 'ouroboros-sdk'
const handleSaved = (order) => console.log('订单更新', order.id)
const { bootstrap, mount, update, unmount } = initMicroApp({
async afterMount() {
EventBus.subscribe('orders:saved', handleSaved)
},
async afterUnmount() {
EventBus.unsubscribe('orders:saved', handleSaved)
},
})
export { bootstrap, mount, update, unmount }
// 保存成功后的交互处理函数中调用:
export function notifySaved(order) {
EventBus.publish('orders:saved', { id: order.id })
}
取消监听需传入原处理函数。组件卸载时使用 unsubscribe,避免移除其他页面的监听。
当前平台支持第三个参数作为过滤器:EventBus.subscribe(event, handler, filter),过滤器返回 true 才通知处理函数。SDK 类型声明的三参数顺序与运行时存在差异;需要兼容不同基座版本时,使用双参数订阅,并在 handler 内判断条件。