Example of unplugin-auto-expose
main-to-renderer pattern?
#921
Answered
by
cawa-93
TheAutomaTom
asked this question in
Q&A
-
Do we have an example pattern for main-to-renderer using cawa-93/unplugin-auto-expose? This is where I'm at with a // The app menu command that originates this ipc event...
{
label: "File",
submenu:[{
label: "Open File...",
click: async function() {
const _filePath = await pickFile();
if (_filePath === null) return;
fs.readFile(_filePath, "utf8", (err: unknown, data: unknown) => {
console.dir(data);
project = JSON.parse(data as string);
});
browserWindow!.webContents.send("load-project-file", project);
}
}]
} // preload/index.ts
import { ipcRenderer } from "electron";
const onLoadProjectFile = (callback: any) => ipcRenderer.on("load-project-file", callback);
export { onLoadProjectFile };
// I believe this is the equivalent without unplugin-auto-expose...
// contextBridge.exposeInMainWorld("electronAPI", {
// onLoadProjectFile: (callback: any) => ipcRenderer.on("load-project-file", callback)
// }); // App.vue
import type { Project } from "./../../ipc-models/Takeoff/Takeoff";
const project = ref({ projectName: "Unloaded Project", projectClient: "Unloaded Client" } as Project);
onMounted( async () => {
window.addEventListener("DOMContentLoaded", () => {
ipcRenderer.on("load-project-file", (_, value) => {
project.value = value;
});
});
});
Thanks for help, this template has been really interesting to work with! |
Beta Was this translation helpful? Give feedback.
Answered by
cawa-93
Apr 3, 2023
Replies: 1 comment 1 reply
-
I feel you kind a missed up in renderer part. You export // App.vue
import { onLoadProjectFile } from '#preload' // <--
import type { Project } from "./../../ipc-models/Takeoff/Takeoff";
const project = ref({ projectName: "Unloaded Project", projectClient: "Unloaded Client" } as Project);
onMounted( async () => {
window.addEventListener("DOMContentLoaded", () => {
onLoadProjectFile((_, value) => { // <--
project.value = value;
});
});
}); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
TheAutomaTom
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I feel you kind a missed up in renderer part. You export
onLoadProjectFile
frompreload
but don't import it inrenderer
. (BTW I also don't see reasons to listenDOMContentLoaded
inonMounted
hook) Perhaps you means: