跳到内容

useIDBKeyval

类别
导出大小
525 B
@vueuse/integrations
最后更改
5 天前

idb-keyval 的封装。idb-keyval 的封装。

演示

@vueuse/integrations 附加组件中可用。

将 idb-keyval 作为对等依赖安装

bash
npm install idb-keyval@^6

用法

ts
import { useIDBKeyval } from '@vueuse/integrations/useIDBKeyval'

// bind object
const { data: storedObject, isFinished } = useIDBKeyval('my-idb-keyval-store', { hello: 'hi', greeting: 'Hello' })

// update object
storedObject.value.hello = 'hola'

// bind boolean
const flag = useIDBKeyval('my-flag', true) // returns Ref<boolean>

// bind number
const count = useIDBKeyval('my-count', 0) // returns Ref<number>

// awaiting IDB transaction
await count.set(10)
console.log('IDB transaction finished!')

// delete data from idb storage
storedObject.value = null

类型声明

typescript
export interface UseIDBOptions extends ConfigurableFlush {
  /**
   * Watch for deep changes
   *
   * @default true
   */
  deep?: boolean
  /**
   * On error callback
   *
   * Default log error to `console.error`
   */
  onError?: (error: unknown) => void
  /**
   * Use shallow ref as reference
   *
   * @default false
   */
  shallow?: boolean
  /**
   * Write the default value to the storage when it does not exist
   *
   * @default true
   */
  writeDefaults?: boolean
}
export interface UseIDBKeyvalReturn<T> {
  data: RemovableRef<T>
  isFinished: ShallowRef<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>

源码

源码演示文档

贡献者

Anthony Fu
IlyaL
Anthony Fu
OrbisK
Fernando Fernández
Oleksandr Hyriavets
Doctorwu
Abdallah Alhaddad
Jimmy Sullivan
sun0day
Harmony Scarlet

更新日志

v12.8.0 于 3/5/2025
7432f - 特性(types): 弃用 MaybeRefMaybeRefOrGetter,支持 Vue 的原生类型 (#4636)
v12.3.0 于 1/2/2025
59f75 - 特性(toValue): 弃用 @vueuse/shared 中的 toValue,支持 Vue 的原生类型
v12.0.0-beta.1 于 11/21/2024
0a9ed - 特性!: 移除 Vue 2 支持,优化打包和清理 (#4349)
v10.9.0 于 2/27/2024
1b67d - 修复: 使用 toRaw 而不是覆盖原始对象 (#3805)
v10.8.0 于 2/20/2024
a086e - 修复: 更严格的类型
v10.4.0 于 8/25/2023
77a86 - 特性(useIdbKeyval): 等待 IDB 写入的能力 (#3338)
v10.0.0-beta.4 于 4/13/2023
4d757 - 特性(types)!: 重命名 MaybeComputedRefMaybeRefOrGetter
0a72b - 特性(toValue): 重命名 resolveUnreftoValue
v10.0.0-beta.0 于 3/14/2023
ef281 - 特性!: 返回格式已更改,添加 isFinished (#2474)

根据 MIT 许可证发布。