如何避免electron中dialog组件回调函数踩坑?
- 内容介绍
- 文章标签
- 相关推荐
本文共计445个文字,预计阅读时间需要2分钟。
在Electron 10之前,我们通过使用`dialog`模块来选择文件时,可以添加`callback`来获取所选文件的路径。然而,Electron 10更新后,这一过程发生了变化,现在我们使用Promise对象来获取结果。以下是简化的修改内容:
在Electron 10之前,获取文件路径的代码如下:
javascriptconst { dialog }=require('electron');
dialog.showOpenDialog({ properties: ['openFile']}).then(result=> { if (!result.canceled) { const filePath=result.filePaths[0]; console.log('Selected file:', filePath); }});
Electron 10更新后,获取文件路径的代码变为:
javascriptconst { dialog }=require('electron');
dialog.showOpenDialog({ properties: ['openFile']}).then(result=> { if (result.canceled) { return; } const filePath=result.filePaths[0]; console.log('Selected file:', filePath);});
踩坑分析
之前版本使用dialog时选择文件时,可以加入callback,来获取被选择文件的路径,而electron10更新后发生了改动,采用了Promise对象来获取结果。
本文共计445个文字,预计阅读时间需要2分钟。
在Electron 10之前,我们通过使用`dialog`模块来选择文件时,可以添加`callback`来获取所选文件的路径。然而,Electron 10更新后,这一过程发生了变化,现在我们使用Promise对象来获取结果。以下是简化的修改内容:
在Electron 10之前,获取文件路径的代码如下:
javascriptconst { dialog }=require('electron');
dialog.showOpenDialog({ properties: ['openFile']}).then(result=> { if (!result.canceled) { const filePath=result.filePaths[0]; console.log('Selected file:', filePath); }});
Electron 10更新后,获取文件路径的代码变为:
javascriptconst { dialog }=require('electron');
dialog.showOpenDialog({ properties: ['openFile']}).then(result=> { if (result.canceled) { return; } const filePath=result.filePaths[0]; console.log('Selected file:', filePath);});
踩坑分析
之前版本使用dialog时选择文件时,可以加入callback,来获取被选择文件的路径,而electron10更新后发生了改动,采用了Promise对象来获取结果。

