全局事件总线
This commit is contained in:
parent
98a05b1f6a
commit
a35af8c40c
@ -3,7 +3,7 @@ const event: { [key: string]: Function } = {}
|
||||
export default {
|
||||
/** 绑定一个事件 */
|
||||
$on(eventName: string, eventFun: Function) {
|
||||
if (event.hasOwnProperty('key1')) {
|
||||
if (event.hasOwnProperty(eventName)) {
|
||||
throw new Error(`存在事件 => ${eventName}`)
|
||||
} else {
|
||||
event[eventName] = eventFun
|
||||
@ -11,7 +11,7 @@ export default {
|
||||
},
|
||||
/** 解绑一个事件 */
|
||||
$off(eventName: keyof typeof event) {
|
||||
if (event.hasOwnProperty('key1')) {
|
||||
if (event.hasOwnProperty(eventName)) {
|
||||
delete event[eventName]
|
||||
} else {
|
||||
throw new Error(`不存在事件 => ${eventName}`)
|
||||
@ -19,7 +19,7 @@ export default {
|
||||
},
|
||||
/** 触发一个事件 */
|
||||
$emit<T>(eventName: keyof typeof event, ...args: any):T {
|
||||
if (event.hasOwnProperty('key1')) {
|
||||
if (event.hasOwnProperty(eventName)) {
|
||||
return event[eventName](...args)
|
||||
} else {
|
||||
throw new Error(`不存在事件 => ${eventName}`)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user