最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

电子不显示任何内容

网站源码admin46浏览0评论

电子不显示任何内容

电子不显示任何内容

我正在使用 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”

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

请帮忙谢谢!

回答如下:

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论