首页 > 解决方案 > 如何在电子 + 反应环境中使用使用对话框?

问题描述

克隆回购并尝试

我正在使用 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,但无济于事。

标签: javascriptnode.jsreactjselectron

解决方案


推荐阅读