【发布时间】:2021-11-29 18:24:12
【问题描述】:
尝试学习 svelte 并使用 dexie。有没有更好的方法从 indexedDB 获取数据到数组中?
我的functions.js文件
export const db = new Dexie("todos");
db.version(1).stores({
todo: "++id,note",
});
App.svelte
<script>
import { onMount } from "svelte";
import { db } from "./functions";
let newnote = "";
let addnote = (e) => {
db.todo.add({ note: newnote });
db.todo.toArray().then(items => {
todos = items;
});
};
$:todos = [];
let start = () => {
db.todo.toArray().then(items => {
todos = items;
});
};
onMount(() => {
start();
});
</script>
<main>
<h1 id="title">ToDo</h1>
<input type="text" id="newnote" bind:value={newnote} />
<input type="button" value="Add" on:click={addnote} />
<br>
{#each todos as todo}
{todo.note} <br>
{/each}
</main>
顺便说一句,你能把这段代码隐藏在应用程序中吗?如果是这样,怎么做?还是没有必要?
【问题讨论】: