【问题标题】:How to pass data from Electron to HTML如何将数据从 Electron 传递到 HTML
【发布时间】:2020-08-03 11:39:48
【问题描述】:

我创建了一个新的电子应用程序。

  1. 在 index.js 中,我使用节点文件系统加载数据。
app.on("ready", ()=>{
   createWindow();
   //data is just any big json object nothing special just static data
   const data = Loader.loadData();
 })
  1. createWindow() 只是创建一个加载 index.html 的窗口
  2. 在 index.html 中我链接到一个脚本
<script defer src="main.js"></script> 
  1. 如何从 main.js 访问const data

如果我尝试在 main.js 中使用 require ,它会起作用,因为它运行在不同的线程上,而不是用节点初始化,更像是一个实际的浏览器窗口。但是有没有办法将数据从 index.js 传递到 main.js

只是一个想法不知道我对问题的概念是否部分正确

如果您需要更多代码或信息,请询问!

【问题讨论】:

    标签: javascript electron


    【解决方案1】:

    你可以使用 module.export(见article

    在你的 main.js 中

    let data;
    app.on("ready", ()=>{
        createWindow();
        //data is just any big json object nothing special just static data
        data = Loader.loadData();
    })
    
    // Export so you can access it from the renderer thread
    module.exports.getData = () => data;
    

    在你的 index.html 中

    <script>
        const {getData} = require('electron').remote.require('./main.js');
        console.log(getData());
    </script>
    

    【讨论】:

      猜你喜欢
      • 2020-02-23
      • 2020-08-28
      • 2018-01-13
      • 1970-01-01
      • 2020-09-22
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多