数据存储 Storage
Storage API 提供键值对形式的本地数据存储能力。
在线试用
导入
ts
import woo from 'mini-sdk'
// 或
import { storage } from 'mini-sdk'API 列表
setStorage
设置键值对数据。
ts
woo.setStorage(options: SetStorageOptions): Promise<void>参数:
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
key | string | 是 | 键名 |
data | unknown | 是 | 值(支持任意可序列化数据) |
ts
// 存储字符串
await woo.setStorage({ key: 'username', data: 'Alice' })
// 存储对象
await woo.setStorage({
key: 'userProfile',
data: { name: 'Alice', age: 25, tags: ['dev', 'design'] },
})
// 存储数组
await woo.setStorage({
key: 'recentSearches',
data: ['mini-sdk', 'bridge', 'router'],
})getStorage
读取指定键的数据。
ts
woo.getStorage<T>(options: GetStorageOptions<T>): Promise<GetStorageResult<T>>参数:
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
key | string | 是 | 键名 |
返回值:
ts
interface GetStorageResult<T = unknown> {
data: T // 存储的值
}ts
// 读取字符串
const { data: username } = await woo.getStorage({ key: 'username' })
console.log(username) // "Alice"
// 带泛型的类型安全读取
interface UserProfile {
name: string
age: number
tags: string[]
}
const { data: profile } = await woo.getStorage<UserProfile>({
key: 'userProfile',
})
console.log(profile.name) // "Alice"removeStorage
删除指定键的数据。
ts
woo.removeStorage(options: RemoveStorageOptions): Promise<void>参数:
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
key | string | 是 | 要删除的键名 |
ts
await woo.removeStorage({ key: 'username' })clearStorage
清除所有存储数据。
ts
woo.clearStorage(options?: ClearStorageOptions): Promise<void>ts
await woo.clearStorage()WARNING
clearStorage 会清除该小程序的所有存储数据,请谨慎使用。
完整示例:Token 管理
vue
<script setup>
import { ref, onMounted } from 'vue'
import woo from 'mini-sdk'
const TOKEN_KEY = 'auth_token'
const USER_KEY = 'user_info'
const user = ref(null)
async function saveAuth(token, userInfo) {
await Promise.all([
woo.setStorage({ key: TOKEN_KEY, data: token }),
woo.setStorage({ key: USER_KEY, data: userInfo }),
])
}
async function loadUser() {
try {
const { data } = await woo.getStorage({ key: USER_KEY })
user.value = data
} catch {
user.value = null
}
}
async function logout() {
await Promise.all([
woo.removeStorage({ key: TOKEN_KEY }),
woo.removeStorage({ key: USER_KEY }),
])
user.value = null
}
onMounted(loadUser)
</script>
<template>
<div v-if="user">{{ user.name }}</div>
<button v-else @click="saveAuth('token', { name: 'Alice' })">登录</button>
<button @click="logout">退出</button>
</template>tsx
import { useState, useEffect } from 'react'
import woo from 'mini-sdk'
const TOKEN_KEY = 'auth_token'
const USER_KEY = 'user_info'
function AuthManager() {
const [user, setUser] = useState(null)
useEffect(() => {
woo.getStorage({ key: USER_KEY })
.then(({ data }) => setUser(data))
.catch(() => setUser(null))
}, [])
async function login() {
await Promise.all([
woo.setStorage({ key: TOKEN_KEY, data: 'token' }),
woo.setStorage({ key: USER_KEY, data: { name: 'Alice' } }),
])
setUser({ name: 'Alice' })
}
async function logout() {
await Promise.all([
woo.removeStorage({ key: TOKEN_KEY }),
woo.removeStorage({ key: USER_KEY }),
])
setUser(null)
}
return (
<>
{user ? <div>{user.name}</div> : <button onClick={login}>登录</button>}
<button onClick={logout}>退出</button>
</>
)
}类型定义
ts
interface SetStorageOptions {
key: string
data: unknown
success?: () => void
fail?: (err: BridgeError) => void
complete?: () => void
}
interface GetStorageOptions<T = unknown> {
key: string
success?: (res: GetStorageResult<T>) => void
fail?: (err: BridgeError) => void
complete?: () => void
}
interface GetStorageResult<T = unknown> {
data: T
}
interface RemoveStorageOptions {
key: string
success?: () => void
fail?: (err: BridgeError) => void
complete?: () => void
}
interface ClearStorageOptions {
success?: () => void
fail?: (err: BridgeError) => void
complete?: () => void
}API 速查表
| 方法 | 说明 | 签名 |
|---|---|---|
setStorage | 存储数据 | ({ key, data }) → Promise<void> |
getStorage | 读取数据 | ({ key }) → Promise<{ data }> |
removeStorage | 删除指定 key | ({ key }) → Promise<void> |
clearStorage | 清除所有数据 | () → Promise<void> |
getStorageInfo | 获取存储信息 | () → Promise<{ keys, currentSize, limitSize }> |
setStorageSync | 同步存储 | (key, data) → void |
getStorageSync | 同步读取 | (key) → any |
removeStorageSync | 同步删除 | (key) → void |
clearStorageSync | 同步清除 | () → void |