javascript - (ipcMain Eletronjs) 事件未定义
问题描述
最近我一直在开发一个加密文件的 Windows 应用程序(使用 Electronjs)。我想要它,这样当我按下渲染器中的第一个按钮时,它会发送到主进程,要求它打开打开文件对话框。我试过这段代码。
渲染器.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]);
}
);
当我尝试这段代码时,它出现了一个错误,指出未定义事件。那么我做错了什么?
解决方案
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]);
}
);
推荐阅读
- sql - 计算每个多边形中的点数
- rest - How to generate token for rest call in codeigniter?
- python - Pandas 不会将分类数据 [sex] 更改为数值 [0/1]
- javascript - Angular 7 如何将内联 JS 脚本包含到组件中?
- sql - 如何将mysql binlog中的SQL转换为html?
- javascript - 类型检查后类型上不存在属性
- python - pandas pos_explode - 未嵌套的数组列但保留索引
- f# - null with cast vs Unchecked.defaultof?
- css - @mixin 通过使用参数选择一个特定的占位符
- java - SBT 在 jar 中包含二进制文件