我正在开发一个使用 Monaco Editor 的 Electron 项目。
要加载文件,我使用以下代码:
dialog.showOpenDialog({}, (files) => {
if(files && files.length > 0) {
fs.readFile(files[0], 'utf8', (err, res) => {
if (!err) {
editor.setModel(monaco.editor.createModel(res, 'javascript'));
}
})
}
})
这个问题是我已经知道我需要加载的文件的位置和文件名。
所以我的问题是...如何加载文件而不弹出询问文件的对话框?
最佳答案
不显示对话框...?只需将 files[0]
替换为已知的文件名和路径即可。
例如
fs.readFile('/path/to/your/file', 'utf8', (err, res) => {
if (!err) {
editor.setModel(monaco.editor.createModel(res, 'javascript'));
}
})
关于Javascript Electron/Monaco 编辑器无需对话框即可加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44000667/