如何在其他ipc渲染文件中使用 Electron.js 的 webContents.send 函数?
我试图将 IPC 函数与电子中的 main.js 文件分开,因为它太长了,
我如何在不同的 js 文件而不是 main.js 中使用此 webContents.send
mainWindow.webContents.send("search",recordset.recordset)
它显示此错误 无法读取未定义的属性(读取“webContents”)
I am trying to separate the IPC function from the main.js file in electron because it gets too long
how can I use this webContents.send in different js file not in main.js
mainWindow.webContents.send("search",recordset.recordset)
it shows this error
Cannot read properties of undefined (reading 'webContents')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关注点分离将是您的首要任务。为此,您可以使用
setters
和getters
。在分离/重构代码之前,您会发现您的
main.js
文件可能会增长到巨大的大小。使用诸如此类的技术将允许您将代码分割成易于管理、可读、单一职责的代码段。如果您还没有这样做,那么让我们将
mainWindow
的构造从main.js
文件移到它自己的文件中。main.js
(主线程)在自己的文件中管理
mainWindow
对象可以将我们的代码分成更具可读性和可管理性的块。main-window.js
(主线程)最后,在需要引用
mainWindow
对象的文件(或任何其他文件)中,只需require
您的main-window.js
文件并调用get()
函数。任意文件
(主线程)Separation of concerns will be your number one priority here. To achieve this, you can use
setters
andgetters
.Prior to separating / refactoring your code, you will find that your
main.js
file can grow to an enormous size. Using techniques such as this will allow you to split up your code into easily manageable, readable, single responsibility segments of code.If you haven’t done so already, let's move construction of your
mainWindow
out of themain.js
file and into its own file.main.js
(main thread)Having management of the
mainWindow
object in its own file separates our code into more readable and manageable chunks.main-window.js
(main thread)Finally, in your file (or any other file) that requires reference to your
mainWindow
object, justrequire
yourmain-window.js
file and call theget()
function.any-file
(main thread)