繁体   English   中英

无法在电子中关闭窗口

[英]Can't close a Window in electron

所以我正在学习关于电子的教程,但我被困在这个阶段,我无法使用关闭按钮来关闭当前窗口,有人可以帮忙吗?

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>

CSS代码:

body {
background:#DFDFDF;
color:#000;
overflow:hidden;
}

p.notify {
background:#C3C3C3;
margin:0;
padding: 20px;
text-transform:uppercase;
-webkit-app-region: drag;
}

.row2 {
width:100%;
display: grid;
grid-template-columns: auto auto;
padding:20px;
}

input {
padding:12px;
}

#closeBtn {
cursor:pointer;
padding:20px;
text-decoration:underline;
}

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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