我通过electron的preload传了一个class到我的前端项目,但是我创建不了实例?

I passed a class to my front-end project through electron's preload, but I couldn't create an instance?

显示的错误:

Can't the class constructor ABC be called without "new"

前端:

我创建了一个 class 并像这样使用预加载:

const { contextBridge } = require('electron')
class ABC {
    constructor() {
        this.item = {}
    }

    update() {
        console.log(`<<<<2021年09月16日 13:56:33>>>>`, this)
    }
}
contextBridge.exposeInMainWorld('$electron', {
    ABC
})

我在前端得到了 ABC:

const {ABC } = window.$electron
const abc = new ABC()

但是控制台抛出一个错误 Class constructor ABC cannot be invoked without 'new'

您不能通过 contextBridge.

公开对象等复杂类型

只有被视为 'simple' 或函数的对象属性可用。文档指出:

The api provided to exposeInMainWorld must be a Function, string, number, Array, boolean, or an object whose keys are strings and values are a Function, string, number, Array, boolean.

contextBridge 会破坏您的 class 方法并且只保留属性。该文档还指出:

Function values are proxied to the other context and all other values are copied and frozen. Any data / primitives sent in the API become immutable and updates on either side of the bridge do not result in an update on the other side.

这是 classes 不起作用的另一个原因,它们通常通过引用传递。

查看支持的tableparameters, errors and return types