Project: A Desktop Document Manager
🎯 Project goal
Build a small document manager: users select text files, the page shows a file list and read status, and the current documents can be exported as a PDF. This example establishes a complete desktop workflow without a complex backend.
🧱 Divide the responsibilities
| Feature | Responsible layer |
|---|---|
| Page buttons, lists, and status | Vue renderer process |
Exposing openFile and exportReport | Preload script |
| Opening files, reading contents, and saving PDFs | Main process |
| Application windows, error handling, and lifecycle | Electron main process |
The page only needs to know whether a request succeeded. It does not access file paths or system modules directly.
🛠 Design a minimal API
const { contextBridge, ipcRenderer } = require('electron/renderer');
contextBridge.exposeInMainWorld('electronAPI', {
openTextFile: () => ipcRenderer.invoke('file:open-text'),
exportReport: data => ipcRenderer.invoke('report:export', data),
});
Each method corresponds to one specific action. The main process must still validate the type and length of data; requests from your own page must not bypass validation.
🖥 Vue page state
<script setup>
import { ref } from 'vue';
const files = ref([]);
const status = ref('Ready to import');
async function openFile() {
status.value = 'Reading';
try {
const file = await window.electronAPI.openTextFile();
if (!file) {
status.value = 'Canceled';
return;
}
files.value.push(file);
status.value = 'Read complete';
} catch {
status.value = 'Read failed';
}
}
</script>
<template>
<main>
<button type="button" @click="openFile">Import text</button>
<p>Status: {{ status }}</p>
<ul>
<li v-for="file in files" :key="file.name">
{{ file.name }}
</li>
</ul>
</main>
</template>
Focus on state changes for now, without adding complex list editing, databases, or permission models. As your project needs these features, introduce them as separate modules.
📄 Report saving and delivery checks
When exporting a report, the main process generates the PDF while the page displays the exporting, success, or failure state. Then check the following in order:
- Canceling selection does not leave unnecessary error messages on the page.
- After a file read fails, the page can still import other files.
- Report contents come from the current documents rather than hard-coded sample data.
- The export window closes after both success and failure.
- The packaged app can still locate the Vue page and report resources.
🧾 Project summary
- Vue manages the page and its state; Electron provides desktop capabilities.
- The preload script exposes system capabilities through small APIs.
- The main process validates arguments and handles files and PDFs.
- The page must display waiting, canceled, success, and failure states.
- Before release, recheck resources and file workflows in the packaged app.
❓ Questions and answers
Q1: Why not let Vue read files directly?
That would undermine process boundaries and security settings. Exposing an API through a preload script makes permissions easier to control.
Q2: Why not connect a database at the start of the project?
This course first practices Electron's core end-to-end workflow. A database introduces extra concepts; beginners should first understand file import and status feedback.
🧪 Practice
Add an “Export” button to each list item and call the reserved exportReport API:
<button type="button">
<!-- Write your code here -->
</button>
🎉 You have completed a basic desktop workflow from file import to report saving!
