电子不显示任何内容

编程入门 行业动态 更新时间:2024-10-04 17:24:06

电子不显示任何<a href=https://www.elefans.com/category/jswz/34/1771374.html style=内容"/>

电子不显示任何内容

我正在使用 node.js 和 electron 构建一个应用程序。

这是我的代码 主.js

const { app, BrowserWindow, ipcMain } = require('electron');
const { divine } = require('./controller/currencyExchange');
const searchJsonReady = require('./src/divinePrice.json');

async function createWindow() {
  try {
    const exchange = await divine(searchJsonReady);
    const formatExchange = exchange[0].map(e => parseInt(e) + 'c').join(',');

    let win = new BrowserWindow({
      width: 800,
      height: 600,
      webPreferences: {
        nodeIntegration: true
      }
    });

    win.loadFile('index.html');

    win.webContents.openDevTools();

    win.on('closed', () => {
      win = null;
    });

    win.webContents.on('did-finish-load', () => {
      setTimeout(() => {
        win.webContents.send('exchangeData', formatExchange);
      }, 1000);
    });
  } catch (error) {
    console.log(error);
  }
}

app.whenReady().then(() => {
  createWindow();
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World</title>
  </head>
  <body>
    <h1>Hello World</h1>
    <div id="exchange-data"></div>

    <style>
      #exchange-data {
        background-color: #eee;
        border: 1px solid #ccc;
        padding: 15px;
        color: black;
      }
    </style>

    <script>
      document.addEventListener('DOMContentLoaded', () => {
        const { ipcRenderer } = require('electron');

        ipcRenderer.on('exchangeData', (event, data) => {
          const exchangeDataElement = document.getElementById('exchange-data');
          exchangeDataElement.innerText = data;
        });

        ipcRenderer.send('getExchangeData');
      });
    </script>
  </body>
</html>

应用程序仅显示 Hello world,但缺少 exchangeData。就像下面的 img

formateExchange 是来自其他 API 的一串数据,即“1000c,900c”

我只是学习如何使用电子,尝试构建一个小应用程序进行练习,但是这个错误让我感到非常沮丧。

请帮忙谢谢!

回答如下:

更多推荐

电子不显示任何内容

本文发布于:2024-05-30 14:04:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内容   电子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!