injectIpc.js 676 B

1234567891011121314151617181920212223242526
  1. const { ipcRenderer: ipc } = window.require('electron')
  2. /**
  3. * 异步调用主函数
  4. * @param ipc
  5. * @param channel
  6. * @param param
  7. * @returns {Promise<unknown>}
  8. */
  9. const callMain = (ipc, channel, param) => {
  10. return new Promise((resolve) => {
  11. // 声明渲染进程函数, 用于主进程函数回调, 返回数据
  12. // 调用主进程函数
  13. ipc.send(channel, param)
  14. ipc.once(channel, (event, result) => {
  15. resolve(result)
  16. })
  17. })
  18. }
  19. export default {
  20. install(Vue) {
  21. Vue.prototype.$ipc = ipc // 全局注入ipc
  22. Vue.prototype.$callMain = (channel, param) => callMain(ipc, channel, param) // 全局注入调用主进程函数的方法
  23. }
  24. }