内容"/>
电子不显示任何内容
我正在使用 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”
我只是学习如何使用电子,尝试构建一个小应用程序进行练习,但是这个错误让我感到非常沮丧。
请帮忙谢谢!
回答如下:更多推荐
电子不显示任何内容
发布评论