javascript - 如何在电子 + 反应环境中使用使用对话框?
问题描述
(克隆回购并尝试)
我正在使用 node、create-react-app 和 electron 开发桌面应用程序。下面这行,这里推荐
const { dialog } = require('electron').remote
当添加到我的store
反应组件(因此不在电子主进程中)时,会导致以下错误:
TypeError: fs.existsSync is not a function
getElectronPath
F:/freelance/repos/creative-ontology-editor/node_modules/electron/index.js:8
5 | var pathFile = path.join(__dirname, 'path.txt');
6 |
7 | function getElectronPath() {
> 8 | if (fs.existsSync(pathFile)) {
9 | var executablePath = fs.readFileSync(pathFile, 'utf-8');
10 |
11 | if (process.env.ELECTRON_OVERRIDE_DIST_PATH) {
我相信这是因为 React 或 create-react-app 专门阻止/取消了某些 node.js 模块,例如fs
,但我可能是错的。请注意,当我包含上述行时,此错误发生在模块内部,而不是我自己的代码中。electron
我的目标是让我的桌面应用程序能够像 Word 或 Excel 那样将文件保存和加载到用户的机器上。
我已经调用const fs = window.require('fs');
了我的反应组件,我认为我不应该这样做,而且,由于这是在实际electron
模块中index.js
,我确保它也调用它,它确实:var fs = require('fs')
. 当我将 react 组件中的调用切换为const fs = window.require('fs')
.
我还确保webPreferences.nodeIntegration
在我的电子主进程中设置为 true,但无济于事。
解决方案
推荐阅读
- mongodb - MongoDB使特定字段的所有键值成为自己的字段(Python)
- android - 带有成功 URL 标识的多个后备 URL
- mysql - 当列字段达到0时触发删除行?
- javascript - 如果用户在javascript中选中或取消选中复选框,如何添加或减去值?
- javascript - 如何在 react-router-dom 中使用 context api": "^5.2.0" 和 react": "^17.0.2"
- javascript - 在css网格中反应画布不是全尺寸
- linkedin - 有没有办法通过linkedin上的api编辑UGC帖子
- css - 为什么 @mixin 不能在 css 中编译为 @media
- header - 将自定义标头添加到 Owin 中间件中的请求标头
- reactjs - 如何在 React 中处理复选框的状态