[英]How to access js-variable in asp-markup from .js file (both executed on the same page)?
[英]How to pass js-variable from .js file to .html page in Electron?
我是第一次开发电子应用程序。 我使用.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);
}
});
}
}
});
但是有什么方法可以将这个projectFile变量发送到html页面吗? 因为有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>
谢谢!
一种同步解决方案是使用共享对象,例如
// 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>
或者,您可以使用通过ipcMain和ipcRenderer进行消息传递来使其异步。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.