疑难解答和提示

读写本地文件

使用Electron的一大优点是可以访问用户的文件系统。 这使您可以在本地系统上读写文件。 为了避免Chromium的限制并写入应用内部文件,请确保使用electron的API,特别是app.getPath(name)函数。 这种辅助方法可以让你获得系统目录的文件路径,例如用户的桌面,系统临时文件等。

我们可以使用专门为我们的应用保留的userData目录,因此我们可以确信其他程序或其他用户交互不应该篡改此文件空间。

import path from 'path'
import { remote } from 'electron'

const filePath = path.join(remote.app.getPath('userData'), '/some.file')

调试主流程

在开发中运行应用时,您可能已经注意到主进程中提及远程调试器的消息。 自从发布electron@^1.7.2版本以来,electro引入了对Inspect API的远程调试,并且可以通过打开Google Chrome提供的链接, 或通过另一个使用缺省的5858端口远程连接到该进程的调试器轻松访问 ,比如Visual Studio Code。

┏ Electron -------------------

Debugger listening on port 5858.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:5858/22271e96-df65-4bab-9207-da8c71117641

┗ ----------------------------