我们仍在将本课翻译成简体中文。目前您阅读的是English版本。帮助我们翻译

0%

Skip to content

Persistence with localStorage

中级~30 分钟0/4 步骤

Keep the ledger after refresh

Browser memory disappears when a page reloads. localStorage gives a small local-first application durable storage on the same device. Arrays must be serialized to JSON when saved and parsed when loaded.

Parsing can fail if stored data is damaged, so recovery is part of the feature—not an afterthought.

操作说明

1

Create loadTransactions with localStorage.getItem and JSON.parse.

提示: Read STORAGE_KEY, then parse only when raw exists.

2

Recover safely with try/catch and return an empty array on failure.

3

Create saveTransactions(items) with JSON.stringify.

4

Save after every push and render once on startup.

你的代码

html
5
1
2
3
4
5

实时预览

实时预览