最近,我一直在开发一个windows应用程序(使用Electronjs)来加密文件。我想要它,所以当我按下渲染器中的第一个按钮时,它会发送到主进程,要求它打开打开的文件对话框。我试过这个密码。
renderer.js
firstButton.addEventListener("click", openFile)
function openFile() {
ipc.send('open-the-open-dialogue');
}
ipc.on('file-picked', function(event, arg) {
console.log(arg);
}main.js
ipc.on('open-the-open-dialogue',
function () {
var filenames = dialog.showOpenDialogSync({ properties: ['openFile'] });
if(!filenames) {
dialog.showErrorBox("ERROR!", "You didn't pick a file to encrypt.")
}
event.reply('file-opened', filenames[0]);
}
);当我尝试这段代码时,它出现了一个错误,它说事件没有定义。那我做错什么了?
发布于 2020-04-14 16:45:36
你的IPC命令错了。您应该在呈现程序进程上使用ipcRenderer,在主进程上使用ipcMain。
示例:
Main.js ipcMain }=要求(‘电子’)ipcMain.on(‘异步-消息’,(事件,arg) => { console.log(arg) //打印"ping“event.reply(‘异步-应答’,‘乒乓’}) ipcMain.on(‘同步-消息’,(事件,arg) => { console.log(arg) //打印"ping”event.returnValue = 'pong‘}) Renderer.js ( console.log(ipcRenderer.sendSync('synchronous-message',‘ping’)/打印"pong“ipcRenderer.on(‘异步-应答’,(事件,arg) => { console.log(arg) // prints "pong”})
你可以读到关于电子IPC从这里开始的文章
发布于 2020-04-15 02:53:08
ipcRenderer.on('open-the-open-dialogue',
// ipc Listner's callback function should have 2 parameters.
function (event, args) {
var filenames = dialog.showOpenDialogSync({ properties: ['openFile'] });
if(!filenames) {
dialog.showErrorBox("ERROR!", "You didn't pick a file to encrypt.")
}
// You are sending through `file-opened` channel
// But where is listener? Maybe `file-picked`
event.reply('file-picked', filenames[0]);
}
);https://stackoverflow.com/questions/61209263
复制相似问题