Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

window.electron.ipcRenderer.postMessage 报错 #508

Open
4 tasks done
wumingluren opened this issue May 17, 2024 · 2 comments
Open
4 tasks done

window.electron.ipcRenderer.postMessage 报错 #508

wumingluren opened this issue May 17, 2024 · 2 comments

Comments

@wumingluren
Copy link

Describe the bug

  // 1、创建一对 port
  const { port1, port2 } = new MessageChannel()
  console.log('ceshi', port1)
  // 2、给主进程传输消息端口 port1
  window.electron.ipcRenderer.postMessage(
    'port',
    { msg: '我是渲染进程一通过 ipcRenderer.postMessage 发送过来的' },
    [port1]
  )
  // 3、把 port2 赋值给 portToMain,方便其他模块获取
  let portToMain = port2
  // 4、port2 绑定事件监听,之后主进程发送的消息都会在这里接收到
  portToMain.onmessage = (event) => {
    const data = event.data
    console.log('[portToMain]message', data)
  }

报错 Error: Invalid value for transfer
看官网的写法就是这样写的
https://www.electronjs.org/zh/docs/latest/api/ipc-renderer#ipcrendererpostmessagechannel-message-transfer

Electron-Vite Version

最新

Electron Version

最新

Vite Version

最新

Validations

@alex8088
Copy link
Owner

没用正确使用,主进程需要使用ipcMain来处理,这里和electron-vite无关。

https://www.electronjs.org/zh/docs/latest/api/message-port-main

@wumingluren
Copy link
Author

没用正确使用,主进程需要使用ipcMain来处理,这里和electron-vite无关。

https://www.electronjs.org/zh/docs/latest/api/message-port-main

这里只贴了渲染进程的代码,主进程没贴。
在preload/index.js文件里面写是正常的,在vue页面里面写就会报错。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants