简体   繁体   English

NodeJS v15.7.0 | Electron v11.2.3 | 未捕获的错误:未捕获的类型错误:Class 构造函数<class>没有'new'就不能调用,但是?</class>

[英]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).我也用IpcMainIpcRenderer进行了尝试,但我得到了一个不同的错误(带有参数的东西)。

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.

相关问题 Uncaught TypeError:类构造函数Hero不能在没有“ new”的情况下调用 - Uncaught TypeError: Class constructor Hero cannot be invoked without 'new' 反应 - 未捕获的类型错误:Class 构造函数 Firebase 不能在没有“新”的情况下调用 - React - Uncaught TypeError: Class constructor Firebase cannot be invoked without 'new' 未捕获的类型错误:Class 构造函数 Controller 不能在没有“new”的情况下被调用 - Uncaught TypeError: Class constructor Controller cannot be invoked without 'new' 未捕获的类型错误:类构造函数 ScratchStorage 不能在没有“new”的情况下被调用 - Uncaught TypeError: Class constructor ScratchStorage cannot be invoked without 'new' 尝试查看聚合物v2.0应用程序时如何解决错误“未捕获的TypeError:如果没有&#39;new&#39;,则无法调用类构造函数m” - How can I solve the error “Uncaught TypeError: Class constructor m cannot be invoked without 'new'” when trying to view a polymer v2.0 app 错误TypeError:如果没有在Ionic 4中使用“ new”,则无法调用类构造函数EventEmitter - ERROR TypeError: Class constructor EventEmitter cannot be invoked without 'new' in Ionic 4 未捕获的类型错误:Class 构造函数场景不能在没有“新”行的情况下调用:391 of:file:///storage/emulated/0/threejs/libs/physi.js - Uncaught TypeError:Class constructor Scene cannot be invoked without 'new' line:391 of:file:///storage/emulated/0/threejs/libs/physi.js TypeError:如果没有“ new”,则无法调用类构造函数分页 - TypeError: Class constructor Pagination cannot be invoked without 'new' 类型错误:类构造函数 ESLintWebpackPlugin 不能在没有“new”的情况下被调用 - TypeError: Class constructor ESLintWebpackPlugin cannot be invoked without 'new' 类型错误:类构造函数 Wia 不能在没有“new”的情况下被调用 - TypeError: Class constructor Wia cannot be invoked without 'new'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM