electron-store无法使用

来源:8-7 将 Electron store 集成到 App 中

幕布斯5187758

2024-04-21

// app.js

const Store = window.electron.require('electron-store')
const fileStore = new Store()

// preload.js

const { contextBridge } = require('electron')

contextBridge.exposeInMainWorld('electron', {
    fs: require('fs'),
    require: window.require,
})

在渲染进程中new store就报错,electron版本是30.0,electron-store: 6.0.1。新版本中无法在渲染进程中直接使用window.require来引入模块了,所以只好把require模块暴露出来

报错如下

Uncaught Error: Class constructor ElectronStore cannot be invoked without 'new'
写回答

1回答

张轩

2024-04-22

同学你好

请参看文档中关于在 main process 以及 renderer 同时使用的例子:

https://github.com/sindresorhus/electron-store?tab=readme-ov-file#how-do-i-get-store-values-in-the-renderer-process-when-my-store-was-initialized-in-the-main-process


它详细介绍了两种使用方式,关于 invoke 以及  handle 的形式,可以看第二章我们今年才更新的关于Electron 进程通信的三种方式。


0
0

2024更新 Electron+React+七牛云实战跨平台桌面应用

开发一款自动云同步的 Markdown 文件管理软件

1251 学习 · 463 问题

查看课程