ipcRenderer.js 762 B

1234567891011121314151617181920212223242526
  1. const { ipcRenderer: ipc } = window.require && 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.on(channel, (event, result) => {
  14. console.log('[ipcRenderer] [callMain] result:', result)
  15. resolve(result)
  16. })
  17. ipc.send(channel, param)
  18. })
  19. }
  20. export default {
  21. install(Vue) {
  22. Vue.prototype.$ipc = ipc // 全局注入ipc
  23. Vue.prototype.$ipcCallMain = (channel, param) => callMain(ipc, channel, param) // 全局注入调用主进程函数的方法
  24. }
  25. }