Vue + Electron Hands-on Project
In this chapter we will use Vue + Electron to build a note-taking app. This hands-on project is named VueElectronNotes, with features covering multiple windows, a tray, file operations and IPC communication, making it a very suitable project for beginners to practice.
If you are not yet familiar with Vue, you can first refer to:Vue Tutorial.
I. Project Features
- Desktop note-taking app (Notes)
- The main window displays the note list
- Creating a new note pops up a child window
- System tray icon; minimize to tray
- Local file save/read
- Secure communication between the renderer process and the main process
II. Project Initialization
1. Create a Vue project
vue create VueElectronNotes
cd VueElectronNotes
Select the default preset (Babel + ESLint).
2. Install Electron and dependencies
npm install --save-dev electron electron-builder concurrently wait-on
npm install electron-store
electron-storeUsed for local data persistence
III. Main process configuration
electron-main.js:
Example
const
{
app
,
BrowserWindow
,
Tray
,
Menu
,
ipcMain
}
=
require
(
'electron'
)
const
path
=
require
(
'path'
)
const
Store
=
require
(
'electron-store'
)
let mainWindow
,
noteWindow
,
tray
const
store
=
new
Store
(
)
function
createMainWindow
(
)
{
mainWindow
=
new
BrowserWindow
(
{
width
:
800
,
height
:
600
,
webPreferences
:
{
preload
:
path.
join
(
__dirname
,
'preload.js'
)
,
contextIsolation
:
true
,
nodeIntegration
:
false
}
}
)
if
(
process.
env
.
NODE_ENV
===
'development'
)
{
mainWindow.
loadURL
(
'http://localhost:8080'
)
}
else
{
mainWindow.
loadFile
(
'dist/index.html'
)
}
mainWindow.
on
(
'minimize'
,
(
e
)
=>
{
e.
preventDefault
(
)
mainWindow.
hide
(
)
}
)
createTray
(
)
}
function
createNoteWindow
(
)
{
noteWindow
=
new
BrowserWindow
(
{
width
:
400
,
height
:
300
,
parent
:
mainWindow
,
modal
:
true
,
webPreferences
:
{
preload
:
path.
join
(
__dirname
,
'preload.js'
)
,
contextIsolation
:
true
,
nodeIntegration
:
false
}
}
)
noteWindow.
loadFile
(
'note.html'
)
}
function
createTray
(
)
{
tray
=
new
Tray
(
path.
join
(
__dirname
,
'assets/icon.png'
)
)
const
contextMenu
=
Menu.
buildFromTemplate
(
[
{
label
:
'Show Main Window'
,
click
:
(
)
=>
mainWindow.
show
(
)
}
,
{
label
:
'Exit'
,
click
:
(
)
=>
app.
quit
(
)
}
]
)
tray.
setToolTip
(
'VueElectronNotes'
)
tray.
setContextMenu
(
contextMenu
)
}
app.
whenReady
(
)
.
then
(
createMainWindow
)
app.
on
(
'window-all-closed'
,
(
)
=>
{
if
(
process.
platform
!==
'darwin'
)
app.
quit
(
)
}
)
ipcMain.
handle
(
'get-notes'
,
(
)
=>
{
return
store.
get
(
'notes'
,
[
]
)
}
)
ipcMain.
on
(
'save-note'
,
(
event
,
note
)
=>
{
const
notes
=
store.
get
(
'notes'
,
[
]
)
notes.
push
(
note
)
store.
set
(
'notes'
,
notes
)
mainWindow.
webContents
.
send
(
'update-notes'
,
notes
)
}
)
Explanation:
mainWindow: the main window, displays the note listnoteWindow: the child window, used to create a new noteTray: the system tray icon and menuipcMain: the main process receives renderer process messages
IV. Renderer Process and Vue
preload.js:
Example
const
{
contextBridge
,
ipcRenderer
}
=
require
(
'electron'
)
contextBridge.
exposeInMainWorld
(
'electronAPI'
,
{
getNotes
:
(
)
=>
ipcRenderer.
invoke
(
'get-notes'
)
,
saveNote
:
(
note
)
=>
ipcRenderer.
send
(
'save-note'
,
note
)
,
onUpdateNotes
:
(
callback
)
=>
ipcRenderer.
on
(
'update-notes'
,
callback
)
}
)
src/App.vue:
Example
Note list
New note
-
{{ note }}
note.html + note.js:
note.html:
Example
New Note
New note
note.js:
Example
const
saveBtn
=
document.
getElementById
(
'saveBtn'
)
saveBtn.
addEventListener
(
'click'
,
(
)
=>
{
const
note
=
document.
getElementById
(
'note'
)
.
value
if
(
note
)
{
window.
electronAPI
.
saveNote
(
note
)
window.
close
(
)
}
}
)
Explanation:
note.htmlis a pop-up window used to input notesnote.jscalledelectronAPI.saveNoteSend to the main process- The main window will receive the
update-notesmessage to update the list
V. Run the Project
Development mode:
npm run electron:serve
- Vue Dev Server + Electron start simultaneously
- UI can be debugged in real time
Package the app:
npm run electron:build
- Output the executable file to
dist_electron - Supports Windows/macOS/Linux