useIDBKeyval
idb-keyval
的封装。
Demo
Available in the @vueuse/integrations add-on.将 idb-keyval 安装为对等依赖项
bash
npm install idb-keyval@^6
使用方法
ts
import { useIDBKeyval } from '@vueuse/integrations/useIDBKeyval'
// 绑定对象
const { data: storedObject, isFinished } = useIDBKeyval('my-idb-keyval-store', { hello: 'hi', greeting: 'Hello' })
// 更新对象
storedObject.value.hello = 'hola'
// 绑定布尔值
const flag = useIDBKeyval('my-flag', true) // 返回 Ref<boolean>
// 绑定数字
const count = useIDBKeyval('my-count', 0) // 返回 Ref<number>
// 等待 IDB 事务
await count.set(10)
console.log('IDB 事务完成!')
// 从 IDB 存储中删除数据
storedObject.value = null
类型声明
typescript
export interface UseIDBOptions extends ConfigurableFlush {
/**
* 监听深层变化
*
* @default true
*/
deep?: boolean
/**
* 错误回调函数
*
* 默认将错误记录到 `console.error`
*/
onError?: (error: unknown) => void
/**
* 使用浅层 Ref 作为参考
*
* @default false
*/
shallow?: boolean
/**
* 当存储中不存在时,将默认值写入存储
*
* @default true
*/
writeDefaults?: boolean
}
export interface UseIDBKeyvalReturn<T> {
data: RemovableRef<T>
isFinished: Ref<boolean>
set: (value: T) => Promise<void>
}
/**
*
* @param key
* @param initialValue
* @param options
*/
export declare function useIDBKeyval<T>(
key: IDBValidKey,
initialValue: MaybeRefOrGetter<T>,
options?: UseIDBOptions,
): UseIDBKeyvalReturn<T>
Source
贡献者
Anthony Fu
丶远方
CofCat
Anthony Fu
Fernando Fernández
Oleksandr Hyriavets
Doctorwu
Abdallah Alhaddad
Jimmy Sullivan
sun0day
Harmony Scarlet
变更日志
v10.9.0
on 2/27/2024v10.8.0
on 2/20/2024a086e
- fix: stricter typesv10.4.0
on 8/25/2023v10.0.0-beta.4
on 4/13/20234d757
- feat(types)!: rename MaybeComputedRef
to MaybeRefOrGetter
0a72b
- feat(toValue): rename resolveUnref
to toValue
v10.0.0-beta.0
on 3/14/2023v9.6.0
on 11/22/2022v9.5.0
on 11/9/2022