2016-09-14 37 views
2

所以我在main文件main.js中創建了一個函數BrowserWindow。可以說:是否有可能在main.js文件中從web到電子調用函數?

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> 

我可以這樣做嗎?

+0

爲什麼不寫一個單獨的模塊,然後在兩個地方加載它? –

+0

而不是返回HelloWorld。我想創建一個Socket.IO服務器。 @MikeC – Facundo

+1

uhmm,你可以做類似的事情,使用ipcMain和ipcRenderer –

回答

3

是的,你可以。

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

但我建議使用ipcMainipcRenderer送過程之間的數據。

相關問題