electron 网页TodoList应用打包win桌面软件数据持久化
创始人
2024-12-16 05:04:29
0

参考:
electron 网页TodoList工具打包成win桌面应用exe
https://blog.csdn.net/weixin_42357472/article/details/140648621

electron直接打包exe应用,打开网页上面添加的task在重启后为空,历史没有被保存,需要持久化工具保存之前的操作记录

这里测试用electron-store或electron-conf两个持久化工具运行都ESM报错:
https://zhuanlan.zhihu.com/p/706281785
在这里插入图片描述

解决方法:
直接使用 Node.js 的 fs 模块直接读写 JSON 文件。这样可以避免 ES modules 的问题。
1)electron配置文件
main.js

const { app, BrowserWindow, ipcMain } = require('electron') const path = require('path') const fs = require('fs')  const STORAGE_PATH = path.join(app.getPath('userData'), 'todos.json')  function saveTodos(todos) {   fs.writeFileSync(STORAGE_PATH, JSON.stringify(todos)) }  function loadTodos() {   try {     return JSON.parse(fs.readFileSync(STORAGE_PATH, 'utf8'))   } catch (error) {     return []   } }  function createWindow () {   const win = new BrowserWindow({     width: 800,     height: 600,     webPreferences: {       nodeIntegration: true,       contextIsolation: false     }   })    win.loadFile('index.html') }  app.whenReady().then(() => {   createWindow()    app.on('activate', () => {     if (BrowserWindow.getAllWindows().length === 0) {       createWindow()     }   }) })  app.on('window-all-closed', () => {   if (process.platform !== 'darwin') {     app.quit()   } })  // 修改这些 IPC 处理器 ipcMain.on('save-todos', (event, todos) => {   saveTodos(todos) })  ipcMain.handle('load-todos', () => {   return loadTodos() }) 

package.json

{   "name": "todolist-app",   "version": "1.0.0",   "main": "main.js",   "build": {     "appId": "com.yourcompany.todolist",     "mac": {       "category": "public.app-category.productivity"     },     "win": {       "icon": "icons/icon.png",       "target": [         "nsis"       ]     },     "linux": {       "target": [         "AppImage",         "deb"       ]     }   },   "scripts": {     "start": "electron .",     "build": "electron-builder  --win"   },   "keywords": [],   "author": "",   "license": "ISC",   "description": "",   "devDependencies": {     "electron": "^31.2.1",     "electron-builder": "^24.13.3"   },   "dependencies": {} } 

2)网页应用
index.html

                 TodoList            

TodoList

    styles.css

    body {     font-family: Arial, sans-serif;     max-width: 500px;     margin: 0 auto;     padding: 20px; } h1 {     text-align: center; } #todo-form {     display: flex;     margin-bottom: 20px; } #todo-input {     flex-grow: 1;     padding: 10px;     font-size: 16px;     border: 1px solid #ddd;     border-radius: 4px 0 0 4px; } #add-button {     padding: 10px 20px;     font-size: 16px;     background-color: #4CAF50;     color: white;     border: none;     border-radius: 0 4px 4px 0;     cursor: pointer; } #todo-list {     list-style-type: none;     padding: 0; } .todo-item {     display: flex;     align-items: center;     padding: 10px;     background-color: #f9f9f9;     border: 1px solid #ddd;     margin-bottom: 10px;     border-radius: 4px; } .todo-item.completed {     text-decoration: line-through;     opacity: 0.6; } .todo-item input[type="checkbox"] {     margin-right: 10px; } .delete-button {     margin-left: auto;     background-color: #f44336;     color: white;     border: none;     padding: 5px 10px;     border-radius: 4px;     cursor: pointer; } 

    script.js

    const { ipcRenderer } = require('electron')  const todoForm = document.getElementById('todo-form') const todoInput = document.getElementById('todo-input') const todoList = document.getElementById('todo-list')  async function loadTodos() {     const todos = await ipcRenderer.invoke('load-todos')     todos.forEach(todo => {         addTodoToDOM(todo.text, todo.completed)     }) }  function saveTodos() {     const todos = Array.from(todoList.children).map(li => ({         text: li.querySelector('span').textContent,         completed: li.classList.contains('completed')     }))     ipcRenderer.send('save-todos', todos) }  function addTodoToDOM(text, completed = false) {     const li = document.createElement('li')     li.className = 'todo-item' + (completed ? ' completed' : '')     li.innerHTML = `         completed ? 'checked' : ''}>         ${text}              `      li.querySelector('input[type="checkbox"]').addEventListener('change', function() {         li.classList.toggle('completed')         if (li.classList.contains('completed')) {             todoList.appendChild(li)         } else {             todoList.insertBefore(li, todoList.firstChild)         }         saveTodos()     })      li.querySelector('.delete-button').addEventListener('click', function() {         li.remove()         saveTodos()     })      if (completed) {         todoList.appendChild(li)     } else {         todoList.insertBefore(li, todoList.firstChild)     } }  todoForm.addEventListener('submit', function(e) {     e.preventDefault()     if (todoInput.value.trim() === '') return      addTodoToDOM(todoInput.value)     saveTodos()     todoInput.value = '' })  loadTodos() 

    打包:

    npm run build 

    在这里插入图片描述
    完成在dist下生产安装文件,及win-unpacked 无需安装的解压文件
    在这里插入图片描述
    在这里插入图片描述
    点击exe运行即可
    在这里插入图片描述

    相关内容

    热门资讯

    透视脚本!aapoker透视怎... 透视脚本!aapoker透视怎么用(透视)详细插件辅助神器(有挂科普)1、实时aapoker透视怎么...
    第四分钟透视!wepoker好... 第四分钟透视!wepoker好友助力码,(wepoker)总是真的是有挂,介绍教程(有挂解说)1、上...
    我来向大家传授!HH平台挂,h... 我来向大家传授!HH平台挂,hhpoker万能辅助器,2025版教程(有挂总结)1、每一步都需要思考...
    三分钟了解!wpk有辅助器,w... 三分钟了解!wpk有辅助器,wpk有那种辅助,透明教程(有挂总结)1、wpk有那种辅助系统规律教程、...
    透视脚本!aapoker万能辅... 透视脚本!aapoker万能辅助器(透视)详细俱乐部靠谱辅助器(有挂黑科技)1、进入游戏-大厅左侧-...
    玩家必看教程!!hhpoker... 玩家必看教程!!hhpoker底牌透视脚本,德州局hhpoker,科技教程(有挂教程);1)hhpo...
    透视脚本!wepoker透视a... 透视脚本!wepoker透视app下载,(WePoKer)一直存在有挂(详细透视底牌辅助插件)1、w...
    九分钟辅助!wpk作弊最怕三个... 九分钟辅助!wpk作弊最怕三个东西,(WPk)一直真的是有挂(详细透视辅助机制教程)在进入wpk作弊...
    三分钟脚本!aapoker怎么... 三分钟脚本!aapoker怎么控制牌,aapoker怎么控制牌,wepoke教程(有挂规律)1、全新...
    透视脚本!hhpoker免费透... 透视脚本!hhpoker免费透视脚本(底牌)详细透视辅助软件(有挂黑科技)1、hhpoker免费透视...