【问题标题】:How can I access Electron's process object from my react app?如何从我的 react 应用程序访问 Electron 的进程对象?
【发布时间】:2020-07-19 09:22:14
【问题描述】:

我是 Electron 新手,不知道如何从我的 app.js 组件访问 Electron 的进程对象。

这是我的函数 createWindow

function createWindow() {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: { nodeIntegration: true },
  });
  mainWindow.loadURL("http://localhost:3000/");
  mainWindow.on("closed", function () {
    mainWindow = null;
  });
}

这是我的 app.js 组件

class App extends React.Component {
  componentDidMount() {
    console.log("process", process);
  }

  render() {
    return <h1>Hello</h1>;
  }
}

目前,我可以启动应用程序和渲染 app.js,但无法从那里访问进程对象。

【问题讨论】:

  • ipcRenderer 是 React 和 Electron 之间通信的一种方式。你可以像 const ipcRenderer = require('electron').ipcRenderer 那样在你的 React 端导入它。查看此链接stackoverflow.com/a/40251412/7645527

标签: reactjs electron


【解决方案1】:

在尝试了多种方法后,这个简单的解决方案奏效了:

componentDidMount() {
    console.log("process", window.process);
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    相关资源
    最近更新 更多