23 lines
497 B
TypeScript
23 lines
497 B
TypeScript
import mitt from 'mitt';
|
|
|
|
const emitter = mitt();
|
|
|
|
export default {
|
|
install(app) {
|
|
// 发送事件
|
|
app.config.globalProperties.$sendChanel = (event, payload) => {
|
|
emitter.emit(event, payload);
|
|
};
|
|
|
|
// 监听事件(返回取消函数)
|
|
app.config.globalProperties.$recvChanel = (event, handler) => {
|
|
emitter.on(event, handler);
|
|
|
|
// 可选:返回解绑函数,方便使用
|
|
return () => {
|
|
emitter.off(event, handler);
|
|
};
|
|
};
|
|
}
|
|
};
|