Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue + Electron Hands-on Project

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


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


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:


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.html + note.js:

note.html:

Example

 

<p>New Note</p>

 

   

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:


V. Run the Project

Development mode:

npm run electron:serve

Package the app:

npm run electron:build