electron的官方中文文档
有哪些常用类型的弹窗
1. electron一个简单的消息框
const { dialog } = require('electron');dialog.showMessageBox(mainWindow, {type: 'info', // 可选类型有 info, warning, error, questionbuttons: ['OK'], // 按钮文本数组title: '提示信息',message: '这是一个提示信息'
}).then(response => {console.log(`用户点击了第 ${response.response} 个按钮`);
});
2. electron打开文件选择器
dialog.showOpenDialog(mainWindow, {properties: ['openFile', 'openDirectory'] // 可选属性,用于指定打开文件还是目录
}).then(result => {if (!result.canceled) {console.log('选定的文件路径:', result.filePaths);}
});
3. electron显示保存文件对话框
dialog.showSaveDialog(mainWindow, {defaultPath: '~/Documents/newfile.txt', // 默认路径filters: [{ name: 'Text files', extensions: ['txt'] },{ name: 'All Files', extensions: ['*'] }] // 文件过滤器
}).then(result => {if (!result.canceled) {console.log('选定的保存路径:', result.filePath);}
});
4.electron形式警告或错误对话框
dialog.showErrorBox('错误标题', '发生了一个严重错误,请稍后再试。');
5. electron中渲染进程中alert
alert("弹窗提示")
注意:alert的弹窗标题为package.json文件的name字段