Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否可以将main.js文件中的函数从web调用到electron?_Javascript_Node.js_Electron - Fatal编程技术网

Javascript 是否可以将main.js文件中的函数从web调用到electron?

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">

因此,我在主文件main.js中有一个函数,用于创建Electron 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>

常量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执行类似的操作