UNPKG

chooks

Version:

基于 [`@vue/composition-api`](https://github.com/vuejs/composition-api) 的vue hooks函数

44 lines (37 loc) 1.23 kB
### useEmitter > 提供一个的订阅-发布模式机制 #### Type ```ts function useEmitter(): { on(eventType: string, handler: Function): void; once(eventType: string, handler: Function): void; emit(eventType: string, data: any): void; off(eventType: string, handler: Function): void; isSubed(eventType: string, handler: Function): boolean; } ``` #### Return - on(eventType,handler) — 订阅某个事件并传入回调函数 - once(eventType,handler) — 订阅某个事件并传入回调函数,此函数被触发一次后自动移除 - emit(eventType,data) — 发布某个事件,并传入data - off(eventType,handler) — 移除某个事件中该函数的订阅 - isSubed(eventType,handler) — 返回该事件中是否订阅了该函数 #### Example ```js import { useEmitter} from 'chooks' export default { setup(){ const { on, off, emit } = useEmitter() const cb = function(data){ console.log(data) } const cb2 = function(data){ console.log(data) } on("test", cb) on("test", cb2) emit("test") return {} } } ```