顶部下载本节源码
2024最新版本 electron + vite + sqlite 开发收藏夹程序
2024最新版本 electron + vite + sqlite 开发收藏夹程序【续一】
2024最新版本 electron + vite + sqlite 开发收藏夹程序【完结】
上一篇我已经搭建好了基础框架
接下来要做的是
- 项目改造:改造项目结构,修改 vite.config.js & electron/main.js 中的相关配置,以使项目能自动加载所需第三方包,并设计出多页面应用
- 通信:electron 与 vue 之间的 IPC 通信
- 数据库设计: SQLite 初始数据结构
项目改造
删除文件
- 删除 src 目录下 main.js、App.vue、style.css
- 删除根目录下 index.html
添加目录与文件
- src 目录下新建两个目录 index、favroite
- 在新增的两个目录下分别创建 main.js、index.html、App.vue 三个页面
现阶段仅需要简单创建页面,两个文件夹中的三个页面可以相同,内容分别如下
main.js
import { createApp } from 'vue';
import App from './App.vue';
import 'tdesign-vue-next/dist/reset.css';
import 'tdesign-vue-next/es/style/index.css';
const app = createApp(App);
app.mount('#app')
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>我的APP</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="main.js"></script>
</body>
</html>
App.vue
<template>
<t-layout align="center">
<t-space direction="vertical">
<t-header> 关闭 </t-header>
<t-content> index </t-content>
</t-space>
</t-layout>
</template>
修改文件 vite.config.js & electron/main.js
- 修改 vite.config.js 为如下内容
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import {join, resolve} from 'path'
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { TDesignResolver } from 'unplugin-vue-components/resolvers';
const assetsPath = (path) => {
return join('static', path)
}
export default defineConfig({
plugins: [
AutoImport({
resolvers: [TDesignResolver({
library: 'vue-next'
})],
}),
Components({
resolvers: [TDesignResolver({
library: 'vue-next'
})],
}),
vue()
],
base:"./",
root: "./src/pages",
build: {
outDir: resolve(process.cwd(),'build'),
sourcemap: false,
chunkSizeWarningLimit: 1500,
assetsDir: 'static',
minify: 'esbuild',
rollupOptions: {
input: {
index: resolve(__dirname, 'src/pages/index/index.html'),
favroite: resolve(__dirname, 'src/pages/favroite/index.html'),
},
output: {
entryFileNames: assetsPath('js/[name].js'),
chunkFileNames: assetsPath('js/[name].js'),
assetFileNames: assetsPath('css/[name].[ext]'),
compact: true,
manualChunks: (id) => {
if(id.includes("node_modules")) {
return id.toString().split('node_modules/')[1].split('/')[0].toString();
}
}
}
},
emptyOutDir: true,
}
})
- 修改 electron/main.js 为如下内容
const { app, BrowserWindow } = require('electron')
const path = require('node:path')
const isPackaged = app.isPackaged
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'
var mainWindow
app.whenReady().then(() => {
createMainWindow()
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createMainWindow()
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
/**创建主窗口 */
const createMainWindow = () => {
mainWindow = new BrowserWindow({
frame:false,
fullscreenable:false,
fullscreen: false,
maximizable: false,
shadow: true,
hasShadow: true,
resizable: false,
width: 880,
height: 500,
webPreferences:{
nodeIntegration:true,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.on('ready-to-show', () => {
mainWindow.show()
})
if (!isPackaged){
mainWindow.loadURL("http://localhost:5173/index/index.html");
}else{
mainWindow.loadFile(path.resolve(__dirname, '../build/index/index.html'))
}
}
改造到这一步,vscode 终端执行 yarn run electron:dev,
通信
经过一系列改造,需要进行功能上的开发了,我设计的是无边框、多应用模式,涉及到了 electron 的进程间通信
preload.js
在 electron 目录下新建 preload.js,通过预加载脚本暴露 ipcRenderer API,消息从渲染器进程发送到主进程
const { contextBridge, ipcRenderer, shell } = require( "electron" );
contextBridge.exposeInMainWorld("electron", {
ipcRenderer,
shell // 暴露 shell 使项目可调用系统浏览器打开收藏夹网页
})
继续改造
无边框窗口打开后,需要关闭,最小化,最大化,新建渲染器进程窗口等,这里仅演示关闭与新建窗口,其他请查阅 electron 官方文档
修改后的 electron/main.js,实现可创建、关闭窗口功能
const { app, BrowserWindow, ipcMain} = require('electron')
const path = require('node:path')
const isPackaged = app.isPackaged
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'
var mainWindow
app.whenReady().then(() => {
createMainWindow()
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createMainWindow()
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
/**创建主窗口 */
const createMainWindow = () => {
mainWindow = new BrowserWindow({
frame:false,
fullscreenable:false,
fullscreen: false,
maximizable: false,
shadow: true,
hasShadow: true,
resizable: false,
width: 880,
height: 500,
webPreferences:{
nodeIntegration:true,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.on('ready-to-show', () => {
mainWindow.show()
})
if (!isPackaged){
mainWindow.loadURL("http://localhost:5173/index/index.html");
}else{
mainWindow.loadFile(path.resolve(__dirname, '../build/index/index.html'))
}
}
/**主窗口最小化 */
ipcMain.on('minWindow', (e, data) => {
mainWindow.minimize()
})
/**关闭主窗口 */
ipcMain.on('closeWindow', (e, data) => {
mainWindow.close()
})
/**创建普通子窗口 */
const createOtherWindow = (data) => {
console.log(data)
otherWindow = new BrowserWindow({
height: data.height || 640,
width: data.width || 480,
center: true,
frame:false,
fullscreen:false,
fullscreenable: false,
closable: true,
resizable: false,
maximizable: false,
webPreferences: {
nodeIntegration: true,
webSecurity: false,
webviewTag: true,
enableRemoteModule: true,
nodeIntegrationInWorker: true,
nodeIntegrationInSubFrames: true,
preload: path.join(__dirname, 'preload.js')
}
})
otherWindow.on('ready-to-show', () => {
otherWindow.show()
})
if (!isPackaged){
otherWindow.loadURL("http://localhost:5173/" + data.name +"/index.html");
}else{
otherWindow.loadFile(path.resolve(__dirname, '../build/' + data.name + '/index.html'))
}
}
/**建立普通子窗口 */
ipcMain.on('createOtherWindow', (e, data) => {
createOtherWindow(data)
})
/**关闭普通子窗口 */
ipcMain.on('closeOtherWindow', () => {
otherWindow.close()
})
相对应的需要改造 src/index/App.vue
修改后的App.vue
<script setup>
const ipcRenderer = window.electron.ipcRenderer
//关闭主窗口
const closeWindow = () => {
ipcRenderer.send('closeWindow')
}
//创建子窗口
const createOtherWindow = (e, b) => {
ipcRenderer.send('createOtherWindow', {
height: 720,
width: 1280,
name: "favroite"
})
}
</script>
<template>
<t-layout align="center">
<t-space direction="vertical">
<t-header>
<t-button @click="closeWindow">关闭窗口</t-button>
</t-header>
<t-content>
<t-button @click="createOtherWindow">我的收藏</t-button>
</t-content>
</t-space>
</t-layout>
</template>
vscode 终端执行 yarn run electron:dev 看效果
数据库设计
版面与时间关系,数据库设计留到下一节再讲,喜欢的朋友帮忙点个赞,有任何疑问可以留言