简体   繁体   中英

Function doesn't work when ipcRenderer is used

this is the first time I'm using electron so these could be some dumb questions but I didn't find anything online about it.

So, I have this in my index.html file

<head>
    <script defer src="generate.js"></script>
</head>
<body>
    <label for="role">MY LABEL</label>
        <select name="abc" id="abc" class="abc">
            <option value="0"></option>
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
            <option value="3">Option 3</option>
            <option value="4">Option 4</option>
            <option value="5">Option 5</option>
     <button onclick="generate()">Generate</button>
</body>

and this in my generate.js file

const ipcRenderer = require('electron').ipcRenderer;

const generate = () => {
    ipcRenderer.send('generate', document.querySelector('.abc').value)
}

In theory, whenever someone clicks on the button the generate function in the .js file should run, but it doesn't. I tried some testing and, removing ipcRenderer, the function get's highlighted (meaning it is in use) and the function works. How do I solve this? If some other code is needed I'll share it.

EDIT: When removing ipcRenderer, I tried the code using alert

After some tries I figured out the problem. The external .js file (in this case generate.js ), doesn't operate using NodeJS. To fix this I had to add two lines of code on my index.js like so.

const createWin = () => {
    win = new BrowserWindow({

//these lines fixed the issue
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false
        }
    })

    win.loadFile('index.html')
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM