在Electron.js中进行确认
我想制作一个消息框,其中包含“是”,并且在电子js应用程序中否定了按钮。我试图用对话框在电子中。但这没有用:
const electron = require('electron')
const { dialog } = electron
console.log(dialog) // undefined
const electron = require('electron')
const dialog = electron.remote.dialog
console.log(dialog) // Uncaught Error: Cannot read "dialog" of undefined (remote is undefined)
然后,我尝试使用对话框这是一个模块在NPM中。但这并没有做我想做的事情。当我单击确定或关闭窗口时,没有什么是或否按钮也返回了相同的响应:
const electron = require('electron')
const dialog = require('dialog')
dialog.info('Are you sure?', 'Confirmation', function(exitCode) {
if (exitCode == 0) {
// Should clicked OK (always response)
}
if (exitCode == 1) {
// Should closed window (but never works)
}
})
我做错了什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将要使用
Electron's
方法。
我已经将对话框的创建和管理放在
main.js
文件中。如果您想将其转移到它的自己的文件,这不是问题。您需要做的就是
get()
如果您需要对话框框成为主窗口的孩子。
main.js
(主过程),以进行过程之间的正确通信,我们必须
使用 Inter-Process Communication 。
PRELOAD.JS
(主要进程)最后,您的
index.html
文件将侦听按钮单击。单击后,将消息发送到主过程以打开这
对话框。
一旦从对话框中收到有效的响应,就会将响应发送回渲染过程进行处理。
index.html
(渲染过程)在对话框中使用超过2个按钮,在创建对话框中,您可能需要指定
取消
,并在操作任何操作之前检查所有有效的返回值。You will want to use
Electron's
dialog.showMessageBox();
method.
I have placed the creation and management of your dialog box in the
main.js
file. If you want to move this into itsown file, that's not a problem. All you would need to do is
get()
the (main) window instance if you want your dialogbox to be a child of the main window.
main.js
(main process)For proper communication between processes, we must
use Inter-Process Communication.
preload.js
(main process)Finally, your
index.html
file will listen for a button click. Once clicked, send a message to the main process to openthe
dialog box.
Once a valid response is received from the dialog box, the response is sent back to the render process for processing.
index.html
(render process)To use more than 2 buttons in your dialog box(es), in the creation of your dialog box you may want to designate
a
cancelId
and check for all valid return values before actioning anything.