1

I'm trying to connect with preload and frame.tsx
But, It isn't working.

frame.tsx

function handleButtonClick(id: string) {
    window.electron.send("requestWindowChange", "data") // Error part
    if(id === 'maximize') {
        setIsMaximize(!isMaximize)
    }
}

I simply add it. and...

preload.ts

import {contextBridge, ipcRenderer} from 'electron'

contextBridge.exposeInMainWorld('electron', {
    send: (channel:string, data) => {
        let normalChannels = ['requestWindowChange']
        if (normalChannels.includes(channel)) {
            ipcRenderer.send(channel, data)
        }
    },
    receive: (channel:string, func) => {
        let normalChannels = ['responseWindowChange']
        if (normalChannels.includes(channel)) {
            ipcRenderer.on(channel, (_event, data) => func(data))
        }
    }
})

Also, I'm already making 'preload.ts' to 'preload.js' by using 'tsc' command.

Finally, here is the electron.ts

mainWindow = new BrowserWindow({
    height: 720,
    width: 1280,
    minHeight: 300,
    minWidth: 450,
    frame: false,
    show: false,
    center: true,
    fullscreen: false,
    kiosk: !isDev,
    resizable: true,
    webPreferences: {
        preload: path.join(__dirname, "preload.js")
    }
})

I only copy necessary part.
If you need more code, please comment here.
I really don't know why It isn't working.

  • Here is Error Message.

any Property 'electron' does not exist on type 'Window & typeof globalThis'. Did you mean 'Electron'?ts(2551)

Teddy Bang
  • 61
  • 7
  • 1
    I don't know much Typescript but you might have to declare the new API you added to the window object: https://stackoverflow.com/a/12709880/13752696 – Autumn May 18 '21 at 08:21
  • @Foxeared Thank you for your help! I'm not sure I know this, but thank to your comment, I can get a hint! – Teddy Bang May 20 '21 at 00:38

1 Answers1

3

I'm trying searching and asking for a week, finally I find it.
Just add this at frame.tsx.

declare global {
    interface Window {
        api? : any
    }
}

What a simple it is...

Teddy Bang
  • 61
  • 7