Vue3 Electron 获取用户文档文件夹
我如何获取在桌面上运行的电子应用程序的用户文档文件夹路径。 我已尝试以下操作,但收到应用程序未定义
的错误。
import fs from "fs";
const { app } = require("electron");
export function getFilepath() {
const filepath = app.getPath("userData") + "/settings.json";
return filepath;
}
该代码位于 helpers.js 文件中,该文件通过我的 electron_preload.js 文件导入。
我不知道什么或如何解决这个问题。
import { contextBridge, ipcRenderer } from "electron";
const helpers= require("../src/helpers");
contextBridge.exposeInMainWorld("helpers", helpers);
How can i get the user docs folder path for an electron app running on the desktop.
I've tried the following but get an error that app is undefined
.
import fs from "fs";
const { app } = require("electron");
export function getFilepath() {
const filepath = app.getPath("userData") + "/settings.json";
return filepath;
}
This code lives in a helpers.js file that is being import through my electron_preload.js file.
I have no clue what or how to solve this.
import { contextBridge, ipcRenderer } from "electron";
const helpers= require("../src/helpers");
contextBridge.exposeInMainWorld("helpers", helpers);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您在预加载环境中执行代码,因此它在相应
BrowserWindow
的渲染器进程中运行。但是,app
仅限于主进程的执行范围( source) 这就是您的代码抛出错误的原因。您必须通过 IPC 公开路径。
如需更深入的解释,请参阅官方 Electron IPC 教程。
(附带说明:文件系统的 I/O 可能应该由主进程而不是渲染器完成。从安全角度来看值得考虑。)
Since you're executing the code in your preload environment, it is being run in the renderer process of the corresponding
BrowserWindow
. However,app
is limited to the main process' execution scope (source) which is why your code throws the error.You will have to expose the path via IPC.
For a more in-depth explanation, see the official Electron IPC tutorial.
(As a side note: I/O to the filesystem should probably be done by the main process, not the renderer. Worth considering from a security point of view.)