2024最新版本 electron + vite + sqlite 开发收藏夹程序【续一】

顶部下载本节源码

2024最新版本 electron + vite + sqlite 开发收藏夹程序
2024最新版本 electron + vite + sqlite 开发收藏夹程序【续一】
2024最新版本 electron + vite + sqlite 开发收藏夹程序【完结】

上一篇我已经搭建好了基础框架

接下来要做的是

  1. 项目改造:改造项目结构,修改 vite.config.js & electron/main.js 中的相关配置,以使项目能自动加载所需第三方包,并设计出多页面应用
  2. 通信:electron 与 vue 之间的 IPC 通信
  3. 数据库设计: SQLite 初始数据结构

项目改造

删除文件

  1. 删除 src 目录下 main.jsApp.vuestyle.css
  2. 删除根目录下 index.html

添加目录与文件

  1. src 目录下新建两个目录 indexfavroite
  2. 在新增的两个目录下分别创建 main.jsindex.htmlApp.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

  1. 修改 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,
  }
})

  1. 修改 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 的进程间通信

渲染器进程1
渲染器进程2
渲染器进程3
渲染器进程N
主进程
收藏夹
数据统计
历史记录
...

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 看效果

数据库设计

版面与时间关系,数据库设计留到下一节再讲,喜欢的朋友帮忙点个赞,有任何疑问可以留言

  • 24
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码小涛

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值