从主应用到渲染器在电子应用中发出自定义事件 [英] Emit custom events in electron app from main to renderer

查看:54
本文介绍了从主应用到渲染器在电子应用中发出自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道这是有效的,因为我尝试了它,但是在任何地方都没有记录,所以我问是否可以使用这种做法,所以不必担心将来会停止工作(众所周知,Electron和nodejs会破坏从一个版本到另一个版本)

So I know this works because I tried it, but it's not documented anywhere so I'm asking if it's OK to use this practice, and not worry that it would stop working in the future (Electron and nodejs are known to break things from one version to another)

这是我正在谈论的实践类型:

This is the type of practice I'm talking about:

main.js

app.emit('did-something', param1, param2);

renderer.js(浏览器窗口)

renderer.js (browser window)

const {app} = require('electron').remote;

app.on('did-something', (param1, param2) => {
  $('#whatever').text(param1);
});

基本上,我正在尝试将所有不直接处理HTML的代码(例如数据库交互)移到main.js中,并且我想确保这样做是正确的方法.

Essentially I'm trying to move all the code that doesn't deal with HTML directly, like database interactions, into main.js and I want to make sure this is the right way to do it.

此外,如果我使用自己的方法和属性扩展应用程序对象,还可以吗?

Also, is it ok if I extend the app object with my own methods and properties?

推荐答案

主要过程几乎始终仅应用于创建BrowserWindows和访问文档中标记为只能通过主要过程访问的电子API.

The main process should almost always only be used for creating BrowserWindows and for accessing electron APIs which are marked in the docs as only accessible via the main process.

有关更多信息,请这篇文章主/渲染器之间的差异及其用途的详细信息.Chromium流程体系结构意味着主流程中的任何阻塞代码也将阻塞渲染器.

Check out this article for more details of the differences between the main/renderer and what they are used for. The Chromium process architecture means that any blocking code in the main process will block the renderers too.

所有应用程序代码都应位于渲染进程中,如果您正在执行长时间运行的进程,则应将其撞到Web Worker或其他渲染器进程中.电子远程可以帮助您做到这一点.

All your app code should be in render processes and if you're executing long-running processes these should be bumped into Web Workers or other renderer processes. electron-remote can help you do this.

如果要在主进程和渲染器进程之间进行通信,则应使用文档 API .

If you want to communicate between the main and renderer processes you should use the documented API's.

这篇关于从主应用到渲染器在电子应用中发出自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆