Javascript Can';不要关上电子窗

Javascript Can';不要关上电子窗,javascript,html,electron,Javascript,Html,Electron,所以我跟随一个关于电子的教程,我被困在这个阶段,我无法用关闭按钮来关闭当前窗口,有人能帮我吗 JavaScript代码: const electron = require('electron') const path = require('path') const remote = electron.remote const closeBtn = document.getElementById('closeBtn') closeBtn.addEventListener('click', fu

所以我跟随一个关于电子的教程,我被困在这个阶段,我无法用关闭按钮来关闭当前窗口,有人能帮我吗

JavaScript代码:

const electron = require('electron')
const path = require('path')
const remote = electron.remote

const closeBtn = document.getElementById('closeBtn')

closeBtn.addEventListener('click', function (event) {
var window = remote.getCurrentWindow();
window.close();
})
Html代码:

<body>
    <p class="notify">Notify me when BTC reaches..</p>

    <div class="row2">
        <div>
            <input id="notifyVal" placeholder="USD">
        </div>
        <button id="updateBtn">Update</button>
    </div>

        <a id="closeBtn">Close Window</a><br>

    </div>
    <script src="add.js"></script>
</body>

我不认为我打字有误,因为我甚至复制了代码,但仍然不起作用。我可以创建新窗口,但无法关闭它们。

这可能是因为在主进程中没有将nodeIntegration设置为true。由于您是从渲染器进程(add.js)调用require方法,因此可能需要将nodeIntegration:true添加到主进程(可能是main.js)中的webPreferences中。尝试这样做:

mainWindow = new BrowserWindow({
    width: 500, 
    height: 400,
    frame: false,
    webPreferences: {
      nodeIntegration: true
    }
});

如果控制台中出现任何错误,请告诉我们,它们将帮助调试问题。

必须添加到main.js和index.js或调用BrowserWindow的任何位置
mainWindow = new BrowserWindow({
    width: 500, 
    height: 400,
    frame: false,
    webPreferences: {
      nodeIntegration: true
    }
});