[英]How to pass js-variable from .js file to .html page in Electron?
I'm developing electron app for the first time. 我是第一次开发电子应用程序。 I use function in .js file to find all files in folder and to write all of them to console. 我使用.js文件中的函数来查找文件夹中的所有文件,并将它们全部写入控制台。
fs.readdir(save_path, function(err, files) {
if (err) {
} else {
for(let projectFile of files)
{
fs.readFile(save_path+"\\"+projectFile, function(err, data)
{
if(data != null)
{
console.log(projectFile);
}
});
}
}
});
But is there any way to send this projectFile variable to html page? 但是有什么方法可以将这个projectFile变量发送到html页面吗? Because there is for loop, so I need to use for loop in html too I guess. 因为有for循环,所以我猜我也需要在html中使用for循环。
<div class="col-sm-3 text-center" style="background: #34495e; height:2160px;">
<p class="b wh" style="margin-top: 15%;">Latest projects</p>
<!-- Projects should be displayed here. I guess there should be some kind of for loop -->
<hr class="divider">
</div>
Thank you! 谢谢!
One sync solution would be using a shared object like 一种同步解决方案是使用共享对象,例如
// within main.js
var entries = fs.readdirSync(save_path+"\\"+projectFile);
global.sharedObj = { entries : entries };
// before
app.on('ready', function() ...
<div class="col-sm-3 text-center" style="background: #34495e; height:2160px;">
<p class="b wh" style="margin-top: 15%;">Latest projects</p>
<script>
const {remote} = require('electron');
const entries = remote.getGlobal('sharedObj').entries;
for (var i in entries) {
document.write('<p>' + entries[i] + '</p>');
}
</script>
<hr class="divider">
</div>
Or you can use messaging via ipcMain and ipcRenderer to do it async. 或者,您可以使用通过ipcMain和ipcRenderer进行消息传递来使其异步。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.