初始化(NoticeUtil.ts
)
// NoticeUtil.ts
import { EventEmitter } from "events"
const NotifyManager = new EventEmitter()
export default NotifyManager
发送通知(xxxx_A.ts
)
// xxxx_A.ts
import NotifyManager from "./NoticeUtil"
let data = { "key": "xxxx_msg" }
NotifyManager.emit("xxxx_KEY", data)
接收通知(xxxx_B.ts
)
// xxxx_B.ts
import NotifyManager from "./NoticeUtil"
NotifyManager.addListener("xxxx_KEY", (data: any) => { console.log("====" + data)})
移除通知(xxxx_A.ts
)
// xxxx_A.ts
import NotifyManager from "./NoticeUtil"
useEffect( () => {
return () => { NotifyManager.removeListener("xxxx_KEY", ()=>{}) }
}, [])