有没有可能从web到电子main.js文件中调用一个函数?

所以我在主文件main.js中创build了一个函数BrowserWindow。 让我们说:

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> 

我可以这样做吗?

是的你可以。

在你的主进程中(可能是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> 

但是我build议使用ipcMainipcRenderer在进程间发送数据。