主题
useIpcRenderer
提供 ipcRenderer 及其所有 API,并支持 Vue 响应式。
🌐 Provides ipcRenderer and all of its APIs with Vue reactivity. Available in the @vueuse/electron add-on.
用法
🌐 Usage
ts
import { useIpcRenderer } from '@vueuse/electron'
import { computed } from 'vue'
// enable nodeIntegration if you don't provide ipcRenderer explicitly
// see: https://www.electronjs.org/docs/api/webview-tag#nodeintegration
const ipcRenderer = useIpcRenderer()
// Ref result will return
const result = ipcRenderer.invoke<string>('custom-channel', 'some data')
const msg = computed(() => result.value?.msg)
// remove listener automatically on unmounted
ipcRenderer.on('custom-event', (event, ...args) => {
console.log(args)
})js
import { useIpcRenderer } from '@vueuse/electron'
import { computed } from 'vue'
// enable nodeIntegration if you don't provide ipcRenderer explicitly
// see: https://www.electronjs.org/docs/api/webview-tag#nodeintegration
const ipcRenderer = useIpcRenderer()
// Ref result will return
const result = ipcRenderer.invoke('custom-channel', 'some data')
const msg = computed(() => result.value?.msg)
// remove listener automatically on unmounted
ipcRenderer.on('custom-event', (event, ...args) => {
console.log(args)
})可用方法
🌐 Available Methods
| 方法 | 描述 |
|---|---|
on(channel, listener) | 监听通道。组件卸载时自动移除监听器。 |
once(channel, listener) | 监听通道一次 |
removeListener(channel, listener) | 移除特定监听器 |
removeAllListeners(channel) | 移除通道的所有监听器 |
send(channel, ...args) | 向主进程发送异步消息 |
invoke(channel, ...args) | 发送消息并以 ShallowRef 获取响应 |
sendSync(channel, ...args) | 发送同步消息并以 ShallowRef 获取响应 |
postMessage(channel, message, transfer?) | 发送可转移对象的消息 |
sendTo(webContentsId, channel, ...args) | 发送给特定 webContents |
sendToHost(channel, ...args) | 发送给 webview 主机 |
使用自定义 IpcRenderer
🌐 With Custom IpcRenderer
如果禁用 nodeIntegration,你可以显式传递 ipcRenderer 实例:
🌐 If nodeIntegration is disabled, you can pass the ipcRenderer instance explicitly:
ts
import { useIpcRenderer } from '@vueuse/electron'
import { ipcRenderer } from 'electron'
const ipc = useIpcRenderer(ipcRenderer)