Preload.js 需要问题
最近我开始使用 Electron 框架练习 javascript,但我遇到了 preload.js 功能的问题。也许我不明白如何使用它,但我不能要求一些电子常量,因为它返回未定义的
我的 preload2.js:
const { BrowserWindow } = require("electron");
console.log(BrowserWindow);
在我的窗口控制台中它返回
Undefined
我排除了问题可能是我的预加载未正确执行但它可能是:
我的 main.js:
const { app, BrowserWindow } = require('electron');
function createWindow() {
console.log(app.getAppPath());
const win = new BrowserWindow({
width: 1000,
height: 600,
//Web preferences può utilizzare il preload.js
webPreferences: {
preload: app.getAppPath() + "\\preload2.js",
}
});
win.loadFile('index.html');
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
请帮助我,我失去了理智
Recently I started practising javascript with the Electron framework but i've encountered a problem with the preload.js functionality. Maybe i didn't understand how to use it, but i can't require some of the electron constants because it returns undefined
My preload2.js:
const { BrowserWindow } = require("electron");
console.log(BrowserWindow);
In my window's console it returns
Undefined
I excluded the problem could be that my preload is not correctly executed but it may be:
My main.js:
const { app, BrowserWindow } = require('electron');
function createWindow() {
console.log(app.getAppPath());
const win = new BrowserWindow({
width: 1000,
height: 600,
//Web preferences può utilizzare il preload.js
webPreferences: {
preload: app.getAppPath() + "\\preload2.js",
}
});
win.loadFile('index.html');
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
Please help me i'm losing my mind
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有结构良好的基本示例,很难掌握 Electron 的
preload.js
脚本的正确使用。互联网上的许多示例将预加载脚本的主要功能(即管理主线程和渲染线程之间的通信通道)与这些通道的实现相结合。为了进一步了解您的知识,请仔细阅读 Electron 的流程模型,< a href="https://www. Electronjs.org/docs/latest/tutorial/context-isolation" rel="noreferrer">上下文隔离 和 进程间通信页面。
一旦您阅读了这些页面(但不一定理解它们),让我们开始将它们缝合在一起,以便它们作为一个整体工作。
如前所述,
preload.js
脚本的目的是在主线程和渲染线程之间进行通信。如果向预加载脚本添加大量函数,预加载脚本很快就会变得不堪重负。作为一种替代方法,我使用我的(唯一的)
preload.js
脚本来仅管理 IPC(按通道名称)。这意味着我指定了在主线程和渲染之间传输或接收的白名单通道名称线程。任何不在白名单中的通道名称都将被拒绝通过。您还可以在发送通道名称时发送数据,以便另一端可以接收数据。好的,让我们看一下
preload.js
文件。preload.js
(主线程)在上面的代码中,您将看到通道名称
message:fromRender
和message:toRender
已添加到白名单。现在,在
main.js
文件中,除了预加载脚本的路径之外,一切看起来都很好。让我们改变这一点。main.js
(主线程)您会注意到我添加了 2 个新的代码部分。一种用于向渲染线程发送消息,另一种用于从渲染线程接收特定消息。
最后,让我们向基本的
index.html
文件添加功能。index.html
(渲染线程)启动应用程序后,渲染线程控制台应显示
Hello from the main thread
。单击
Send
按钮后,主线程应显示来自渲染线程的Hello
。如果您需要任何澄清或进一步解释,请告诉我。
The proper use of Electron's
preload.js
script is hard to grasp without a basic well-structured example. Many examples on the internet blend the primary function of the preload script (which is to manage the channels of communication between the main thread and render threads) with implementation of those channels.To further your knowledge, have a good read of Electron's Process Model, Context Isolation and Inter-Process Communication pages.
Once you have read these pages (but not necessarily understood them) let's begin stitching them together so they all work as a whole.
As described earlier, the purpose of the
preload.js
script is to communicate between the main thread and render thread(s). Preload scripts can become overwhelming very quickly if you add a large number of functions to them.As an alternative approach, I use my (one and only)
preload.js
script for management of IPC's (by channel name) only. What this means is that I specific whitelisted channel names that are transmitted or received between the main thread and render thread(s). Any channel names used that are not on the whitelist are denied passage. You can also send data when transmitting the channel name so the data can be received at the other end.Ok, let's look at the
preload.js
file.preload.js
(main thread)In the above code you will see that the channel names
message:fromRender
andmessage:toRender
have been added to the whitelist.Now, in your
main.js
file, everything looks good except for the path to the preload script. Let's change that.main.js
(main thread)You will notice I have added 2 new sections of code. One to send a message to the render thread and one to receive a specific message from the render thread.
Finally, let's add functionality to our basic
index.html
file.index.html
(render thread)Upon starting the application, the render thread console should display
Hello from the main thread
.Upon clicking the
Send
button, the main thread should displayHello from the render thread
.If you need any clarification or further explanation just let me know.