本文介绍了如何从我的网页中调用Electron中的功能/模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我将尝试在简短的段落中描述一个最小化的问题。I'll try to describe a minimized question in short paragraphs.总之,我想使用某些逻辑或从位于我的电子应用程序中的网页(我实际上是在为我的网页包装一个电子应用程序外壳)。In short, I want to use some logic or call some functions in my Electron App from the webpage that is in my Electron App (I am actually wrapping an electron app 'shell' for my webpage).假设我要公开我的电子应用程序中的功能。说,Suppose I want to expose a function in my Electron app. Say,function printNumbers () { console.log(1)}请注意它应该位于我的电子代码中。notice that it should be located in my Electron code.然后,在运行我的应用程序后,我想从我的网页调用此函数(单击我的网页中从网站加载的按钮,然后在我的Electron App中打开一个新窗口)。现在,我想可以使用开发人员控制台检查printNumber是否正常工作。Then after running my app, I'd like to call this function from my webpage(clicking a button in my webpage which is loaded from a website, then open a new window in my Electron App). For now, I think I can check whether printNumber works by using the developer console.我已经检查了如何使用 remote 模块调用电子内部的函数/模块。但是我没有找到调用我在电子代码库中编写的函数的方法。I have checked how to use remote module to call functions/modules inside electron. But I didn't find a way to call a function I write in my electron code base. BTW:我可以启用 nodeIntegration 选项。推荐答案在渲染器进程和主进程之间有两种主要的通信方式。There are two main ways to communicate between the renderer process and the main process. 1。。一种方法是使用远程模块,以要求您从主过程中获取代码。该对象将包含您从主流程代码导出的所有内容。1. One way would be to use the remote module to require the code you want to take from the main process. This object will contain anything you export from your main process code.// main process, for example app/main.jsexports.test = () => console.log('Yay');// renderer process, for example app/renderer.jsconst { remote } = require('electron');const mainProcess = remote.require('./main.js');mainProcess.test(); // 'Yay' 2。。另一种方法是使用进程间通信,用于在主进程和渲染器进程:2. Another way would be to use Inter Process Communication to send/receive events between the main process and the renderer process:// main process, for example app/main.jsmyWindow.webContents.send('my-cool-log-event', 'Yay');// renderer process, for example app/renderer.jsconst { ipcRenderer } = require('electron');ipcRenderer.on('my-cool-log-event', (evt, msg) => console.log(msg)); // 'Yay' 如果要在渲染器进程中触发click事件时,从主进程调用函数,则可以使用两种方法。If you want to call a function from the main process when a click event fires in a renderer process, you can use either approach. 1。// main process, for example app/main.jsexports.onClick = () => console.log('Yay');// renderer process, for example app/renderer.jsconst { remote } = require('electron');const mainProcess = remote.require('./main.js');document .querySelector('#elem') .addEventListener('click', () => { mainProcess.onClick(); }); 2。// main process, for example app/main.jsconst { ipcMain } = require('electron')ipcMain.on('click', () => console.log('do something'));// renderer process, for example app/renderer.jsconst { ipcRenderer } = require('electron');document .querySelector('#elem') .addEventListener('click', () => { ipcRenderer.send('click'); }); 这篇关于如何从我的网页中调用Electron中的功能/模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 06:10