caoxiemeihao / nuxt-electron

Integrate Nuxt and Electron
MIT License
184 stars 21 forks source link

How to access contextBridge in Nuxt #11

Closed redmondking closed 1 year ago

redmondking commented 1 year ago

Hello,

I have been using this but have been unable to access my preload commands in the app. Using window.myAPI.myfunction throws an error of undefined.

How do I resolve this? Does there need to be an additional import for it to work. See example code below.

preload.ts:

const {contextBridge, app} = require('electron')

const windowAPI = {
    closeWindow: () => app.quit
}

contextBridge.exposeInMainWorld("api", windowAPI);

index.vue:

<script setup lang='ts'>
const isFullscreen = ref(false)
const test = () => {
    isFullscreen.value = !isFullscreen.value
}

const quitApp = window.api.closeWindow()

</script>
caoxiemeihao commented 1 year ago

app can only access in Main process.

preload.ts

ipcRenderer.send('close-window')

main.ts

ipcMain.on('close-winow', () => app.quit())