【问题标题】:Electron: Loading a JS file instead of an HTML file into a BrowserWindowElectron:将 JS 文件而不是 HTML 文件加载到 BrowserWindow
【发布时间】:2020-04-06 15:44:34
【问题描述】:

我正在尝试将现有的 redux 应用程序集成到 ElectronJS 中。

样板模板在main.js中有以下代码

mainWindow.maximize() 
// and load the index.html of the app.
mainWindow.loadFile('index.html')

我想将 index.html 更改为我的 React 应用程序的 index.js 文件。

【问题讨论】:

    标签: reactjs electron


    【解决方案1】:

    在 Electron 中,您仍然可以调用 mainWindow.loadFile('index.html')

    即使您正在为应用的视图编写 React 代码,所有 React 应用仍需要在某些时候调用 ReactDOM.render 以将您的应用附加到某个 HTML 文件的 DOM。

    假设您的 index.js 文件是调用 ReactDOM.render() 的文件,那么您需要做的与普通的 React 网络应用没有太大区别。

    // main.js
    // ...
    mainWindow.loadFile('index.html')
    // ...
    
    <!-- index.html -->
    <!-- ... -->
    <body>
      <div id="root"></div>
      <script src="../path/to/index.js"></script>
    </body>
    <!-- ... -->
    
    // index.js
    
    // ...
    const app = <MyRootAppComponent/>
    ReactDOM.render(app, document.getElementById('root'));
    // ...
    

    【讨论】:

      猜你喜欢
      • 2015-10-01
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      • 1970-01-01
      • 2019-07-02
      • 2016-07-25
      • 1970-01-01
      • 2016-11-19
      相关资源
      最近更新 更多