Vue + Electron 実践プロジェクト
この章では、次を使用します。Vue + Electronノートアプリを作成します。この実践プロジェクトの名前はVueElectronNotes、多ウィンドウ、トレイ、ファイル操作、IPC 通信を網羅しており、初心者が練習するのに非常に適したプロジェクトです。
Vue にまだ詳しくない場合は、次を参照してください:Vue チュートリアル。
一、プロジェクト機能
- デスクトップノートアプリ(Notes)
- メインウィンドウにノート一覧を表示
- 新規ノート作成時に子ウィンドウをポップアップ
- システムトレイアイコン、トレイに最小化
- ファイルのローカル保存/読み込み
- レンダラープロセスとメインプロセスの安全な通信
二、プロジェクト初期化
1. Vue プロジェクトの作成
vue create VueElectronNotes cd VueElectronNotes
デフォルトのプリセット(Babel + ESLint)を選択します。
2. Electron と依存関係のインストール
npm install --save-dev electron electron-builder concurrently wait-on npm install electron-store
electron-storeローカルデータの永続化に使用
三、メインプロセス設定
electron-main.js:
例
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: 'メインウィンドウを表示', click: () => mainWindow.show() },
{ label: 終了, 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)
})
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: 'メインウィンドウを表示', click: () => mainWindow.show() },
{ label: 終了, 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)
})
説明:
mainWindow:メインウィンドウ、ノート一覧を表示noteWindow:子ウィンドウ、新規ノート作成用Tray:システムトレイアイコンとメニューipcMain:メインプロセスがレンダラープロセスのメッセージを受信
四、レンダラープロセスと Vue
preload.js:
例
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)
})
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:
例
<template>
<div>
<h1>ノート一覧</h1>
<button @click="openNoteWindow">新規ノート</button>
<ul>
<li v-for="(note, index) in notes" :key="index">{{ note }}</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return { notes: [] }
},
methods: {
openNoteWindow() {
window.open('note.html', '新建笔记', 'width=400,height=300')
}
},
mounted() {
window.electronAPI.getNotes().then((notes) => {
this.notes = notes
})
window.electronAPI.onUpdateNotes((event, notes) => {
this.notes = notes
})
}
}
</script>
<div>
<h1>ノート一覧</h1>
<button @click="openNoteWindow">新規ノート</button>
<ul>
<li v-for="(note, index) in notes" :key="index">{{ note }}</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return { notes: [] }
},
methods: {
openNoteWindow() {
window.open('note.html', '新建笔记', 'width=400,height=300')
}
},
mounted() {
window.electronAPI.getNotes().then((notes) => {
this.notes = notes
})
window.electronAPI.onUpdateNotes((event, notes) => {
this.notes = notes
})
}
}
</script>
note.html + note.js:
note.html:
例
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>新規ノート</title></head>
<body>
<h2>新規ノート</h2>
<textarea id="note"></textarea><br>
<button id="saveBtn">保存</button>
<script src="note.js"></script>
</body>
</html>
<html>
<head><meta charset="UTF-8"><title>新規ノート</title></head>
<body>
<h2>新規ノート</h2>
<textarea id="note"></textarea><br>
<button id="saveBtn">保存</button>
<script src="note.js"></script>
</body>
</html>
note.js:
例
const saveBtn = document.getElementById('saveBtn')
saveBtn.addEventListener('click', () => {
const note = document.getElementById('note').value
if (note) {
window.electronAPI.saveNote(note)
window.close()
}
})
saveBtn.addEventListener('click', () => {
const note = document.getElementById('note').value
if (note) {
window.electronAPI.saveNote(note)
window.close()
}
})
説明:
note.htmlはポップアップウィンドウで、ノートを入力するために使用します。note.js呼び出しelectronAPI.saveNoteメインプロセスに送信- メインウィンドウは受信します
update-notesメッセージでリストを更新
五、プロジェクトの実行
開発モード:
npm run electron:serve
- Vue Dev Server + Electron を同時に起動
- UI をリアルタイムでデバッグ可能
パッケージ化:
npm run electron:build
- 実行可能ファイルの出力先:
dist_electron - Windows/macOS/Linux に対応