查看“Electron+vue+sqlite3”的源代码
←
Electron+vue+sqlite3
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 安装sqlite3 == <pre>npm install better-sqlite3</pre> <br> <span id="创建-sqlite-数据库"></span> == 创建 SQLite 数据库 == 在项目根目录下创建一个名为 database.js 的文件 <pre>const sqlite = require('better-sqlite3') const path = require('path') const dbPath = './database.sqlite' const db = new sqlite(dbPath) const initDb = () => { db.prepare(` CREATE TABLE IF NOT EXISTS todos ( id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT NOT NULL, completed INTEGER NOT NULL DEFAULT 0 ); `).run() } module.exports = { db, initDb, } export { db, initDb, }</pre> <br> == 创建界面 == 在项目根目录下创建一个名为 TodoList.vue 的文件 <pre><template> <div> <h1>Todo List</h1> <form @submit.prevent="addTodo"> <input v-model="newTodoText" type="text" placeholder="Add a new todo"> <button type="submit">Add</button> </form> <ul> <li v-for="todo in todos" :key="todo.id"> <input type="checkbox" v-model="todo.completed" @change="updateTodo(todo)"> <span :class="{ completed: todo.completed }">{{ todo.text }}</span> <button @click="deleteTodo(todo)">Delete</button> </li> </ul> </div> </template> <script> import {db, initDb} from './database.js' initDb(); class Todo { constructor(text, completed = false) { this.text = text this.completed = completed } save() { const stmt = db.prepare(` INSERT INTO todos (text, completed) VALUES (?, ?) `) stmt.run(this.text, this.completed ? 1 : 0) } static findAll() { const stmt = db.prepare(` SELECT * FROM todos `) const rows = stmt.all() return rows.map(row => new Todo(row.text, row.completed)) } static findById(id) { const stmt = db.prepare(` SELECT * FROM todos WHERE id = ? `) const row = stmt.get(id) return row ? new Todo(row.text, row.completed) : null } update() { const stmt = db.prepare(` UPDATE todos SET text = ?, completed = ? WHERE id = ? `) stmt.run(this.text, this.completed ? 1 : 0, this.id) } delete() { const stmt = db.prepare(` DELETE FROM todos WHERE id = ? `) stmt.run(this.id) } } export default { data() { return { newTodoText: '', todos: [], } }, async mounted() { const rows = await Todo.findAll() this.todos = rows }, methods: { async addTodo() { const todo = new Todo(this.newTodoText) todo.save() this.todos.push(todo) this.newTodoText = '' }, async updateTodo(todo) { todo.update() }, async deleteTodo(todo) { todo.delete() this.todos = this.todos.filter(t => t.id !== todo.id) }, }, } </script> <style> .completed { text-decoration: line-through; } </style></pre> <br> <span id="集成-sqlite"></span> == 集成 SQLite == <pre><script setup> import { RouterLink, RouterView } from 'vue-router' import HelloWorld from './components/HelloWorld.vue' import TodoList from './TodoList.vue' </script> <template> <header> <img alt="Vue logo" class="logo" src="@/assets/logo.svg" width="125" height="125" /> <div class="wrapper"> <HelloWorld msg="You did it!" /> <nav> <RouterLink to="/">Home</RouterLink> <RouterLink to="/about">About</RouterLink> </nav> </div> <TodoList></TodoList> </header> <RouterView /> </template> <style scoped> header { line-height: 1.5; max-height: 100vh; } .logo { display: block; margin: 0 auto 2rem; } nav { width: 100%; font-size: 12px; text-align: center; margin-top: 2rem; } nav a.router-link-exact-active { color: var(--color-text); } nav a.router-link-exact-active:hover { background-color: transparent; } nav a { display: inline-block; padding: 0 1rem; border-left: 1px solid var(--color-border); } nav a:first-of-type { border: 0; } @media (min-width: 1024px) { header { display: flex; place-items: center; padding-right: calc(var(--section-gap) / 2); } .logo { margin: 0 2rem 0 0; } header .wrapper { display: flex; place-items: flex-start; flex-wrap: wrap; } nav { text-align: left; margin-left: -1rem; font-size: 1rem; padding: 1rem 0; margin-top: 1rem; } } </style></pre> <br>
返回至“
Electron+vue+sqlite3
”。
导航菜单
个人工具
登录
命名空间
页面
讨论
大陆简体
查看
阅读
查看源代码
查看历史
更多
搜索
导航
首页
基础知识
正则表达式
Markdown
分布式
项目管理
系统集成项目管理基础知识
云原生
Docker
云原生安全
云原生词汇表
十二因素应用
Kubernetes
音频处理
音频合成
Edge-tts
CMS系统
Docsify
VuePress
Mediawiki
自动生成
Marp
CI/CD
GitLab
设计
颜色
平面设计
AI
数字人
操作系统
GNU/Linux
数据库
Mysql
工具
链入页面
相关更改
特殊页面
页面信息