我一直在尝试制作一个简单的程序来使用 Electron 创建和读取文件。 到目前为止,我已经尝试了很多,似乎我为dialog.showOpenDialog提供的回调函数没有被调用。
dialog.showOpenDialog( (filePaths) => {
console.log('this callback is called');
console.log(filePaths);
});
//Directly read a test file
fs.readFile('readtest.txt', 'utf-8', (err, data) => {
if (err) throw err;
console.log(data);
});
这是我的读取按钮处理程序中的代码。 对话框打开,我选择一个文件,但它什么也没做。 但是,我选择的同一文件由 fs.readFile 读取并显示在控制台中。
在我选择文件后,回调似乎没有被调用。
最佳答案
它返回一个 promise ,因此您可以将其与 .then 链接起来:
dialog.showOpenDialog(null, options).then((filePaths) => {
console.log('this callback is called');
console.log(filePaths);
});
关于dialog - Electron dial.showOpenDialog 未运行回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60565120/