[英]NodeJS v15.7.0 | Electron v11.2.3 | Uncaught Error: Uncaught TypeError: Class constructor <Class> cannot be invoked without 'new', but is?
The following webPreferences
properties are enabled in the BrowserWindow
Object I created:在我创建的BrowserWindow
Object 中启用了以下webPreferences
属性:
{
webPreferences:
{
nodeIntegration:false,
enableRemoteModule:false,
contextIsolation:true,
worldSafeExecuteJavaScript:true,
preload:path.join(__dirname,"preload.js"),
webSecurity:true,
webviewTag:false,
allowRunningInsecureContent:false,
nodeIntegrationInSubFrames:false,
nodeIntegrationInWorker:false,
}
}
My preload.ts
:我的preload.ts
:
electron.contextBridge.exposeInMainWorld(
"someapiname",
{
requireClass(): any
{
return require("path/to/module").ClassName
}
}
)
My HTML file loads a Script called html.js
, which contains:我的 HTML 文件加载了一个名为html.js
的脚本,其中包含:
(function()
{
const CLASSNAME = window["someapiname"].requireClass()
const newclass = new CLASSNAME("some param",3)
newclass.aRandomMethod()
})()
But in the Developer Console of Electron I always get:但是在 Electron 的开发者控制台中,我总是得到:
Uncaught Error: Uncaught TypeError: Class constructor CLASSNAME cannot be invoked without 'new'未捕获的错误:未捕获的类型错误:Class 构造函数 CLASSNAME 不能在没有“新”的情况下调用
I don't get it...It asks for the new
but IT is there?我不明白...它要求new
的但它在那里? Is there something I don't know or some kind of restriction of Electron (and no, I do not want nodeIntegration
to be set to true and any other harmful options like enableRemoteModule
)有什么我不知道的或 Electron 的某种限制(不,我不希望nodeIntegration
设置为 true 和任何其他有害选项,如enableRemoteModule
)
There's no problem when I create the class in the preload
script, but then the methods of the Class go missing.当我在preload
脚本中创建 class 时没有问题,但是缺少 Class go 的方法。 I can only access the properties.我只能访问属性。 I also tried it with that IpcMain
and IpcRenderer
but I got a different Error (something with Parameter).我也用IpcMain
和IpcRenderer
进行了尝试,但我得到了一个不同的错误(带有参数的东西)。
I just want to create a Class in the renderer and use its methods.我只想在渲染器中创建一个 Class 并使用它的方法。
https://www.electronjs.org/docs/api/context-bridge#parameter--error--return-type-support https://www.electronjs.org/docs/api/context-bridge#parameter--error--return-type-support
It explicitly states you cannot copy classes / ctors via contextBridge.它明确指出您不能通过 contextBridge 复制类/ctors。
Prototype modifications are dropped.原型修改被丢弃。 Sending classes or constructors will not work.发送类或构造函数将不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.