mirror of
https://github.com/electron-vite/electron-vite-vue
synced 2025-04-25 22:16:59 +08:00
feat: better ipcRenderer
api expose
This commit is contained in:
parent
a9952f16c8
commit
11e9c4bffa
@ -1,14 +1,26 @@
|
|||||||
import { ipcRenderer, contextBridge } from 'electron'
|
import { ipcRenderer, contextBridge } from 'electron'
|
||||||
|
|
||||||
// --------- Expose some API to the Renderer process ---------
|
// --------- Expose some API to the Renderer process ---------
|
||||||
contextBridge.exposeInMainWorld('app', {
|
contextBridge.exposeInMainWorld('ipcRenderer', {
|
||||||
onEvent(cb) {
|
on(...args: Parameters<typeof ipcRenderer.on>) {
|
||||||
const channel = 'main-process-message'
|
const [channel, listener] = args
|
||||||
const channel2 = 'other-ipc-channel'
|
ipcRenderer.on(channel, (event, ...args) => listener(event, ...args))
|
||||||
|
|
||||||
ipcRenderer.on(channel, (_e, ...args) => cb(channel, ...args))
|
|
||||||
ipcRenderer.on(channel2, (_e, ...args) => cb(channel2, ...args))
|
|
||||||
},
|
},
|
||||||
|
off(...args: Parameters<typeof ipcRenderer.off>) {
|
||||||
|
const [channel, ...omit] = args
|
||||||
|
ipcRenderer.off(channel, ...omit)
|
||||||
|
},
|
||||||
|
send(...args: Parameters<typeof ipcRenderer.send>) {
|
||||||
|
const [channel, ...omit] = args
|
||||||
|
ipcRenderer.send(channel, ...omit)
|
||||||
|
},
|
||||||
|
invoke(...args: Parameters<typeof ipcRenderer.invoke>) {
|
||||||
|
const [channel, ...omit] = args
|
||||||
|
ipcRenderer.invoke(channel, ...omit)
|
||||||
|
},
|
||||||
|
|
||||||
|
// You can expose other APTs you need here.
|
||||||
|
// ...
|
||||||
})
|
})
|
||||||
|
|
||||||
// --------- Preload scripts loading ---------
|
// --------- Preload scripts loading ---------
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
|
|
||||||
window.app.onEvent((channel, ...args) => {
|
window.ipcRenderer.on('main-process-message', (_event, ...args) => {
|
||||||
console.log(channel, ...args)
|
console.log('[Receive Main-process message]:', ...args)
|
||||||
})
|
})
|
||||||
|
6
src/vite-env.d.ts
vendored
6
src/vite-env.d.ts
vendored
@ -7,8 +7,6 @@ declare module '*.vue' {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface Window {
|
interface Window {
|
||||||
app: {
|
// expose in the `electron/preload/index.ts`
|
||||||
// Expose in the `electron/preload/index.ts`
|
ipcRenderer: import('electron').IpcRenderer
|
||||||
onEvent: (cb: (channel: string, ...args: any[]) => void) => void
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user