Javascript 是否可以将main.js文件中的函数从web调用到electron?
因此,我在主文件main.js中有一个函数,用于创建Electron BrowserWindow。让我们说:Javascript 是否可以将main.js文件中的函数从web调用到electron?,javascript,node.js,electron,Javascript,Node.js,Electron,因此,我在主文件main.js中有一个函数,用于创建Electron BrowserWindow。让我们说: function HelloWorld(name){ return 'Hello World! said ' + name; } 我可以在Electron加载的html页面中调用它吗 <html> <head> <script type="text/javascript">
function HelloWorld(name){
return 'Hello World! said ' + name;
}
我可以在Electron加载的html页面中调用它吗
<html>
<head>
<script type="text/javascript">
const hello = require('electron').HelloWorld
</script>
</head>
<body onLoad="alert(hello);">
</body>
</html>
常量hello=require('electron')。HelloWorld
我可以吗?是的,你可以
在主流程(可能是main.js)中,将这一行放在主流程中:
global.HelloWorld = function(name){
return 'Hello World! said ' + name;
}
在您的HTML中:
<html>
<head>
<script type="text/javascript">
let {remote} = require('electron');
const hello = remote.getGlobal("HelloWorld")(); // <-- () this is important
</script>
</head>
<body onLoad="alert(hello);">
</body>
</html>
设{remote}=require('electron');
const hello=remote.getGlobal(“HelloWorld”)();//为什么不写一个单独的模块,然后在两个地方加载它呢?而不是返回HelloWorld。我只想创建一个Socket.IO服务器@Mikeuchmm,您可以使用ipcMain和ipcRenderer执行类似的操作