跳到主要内容

useRTDB

类别
导出大小
257 B
Package
@vueuse/firebase
最近更改
3 weeks ago

响应式 Firebase Realtime Database 绑定。 使您可以轻松地始终保持本地数据与远程数据库同步。 可在 @vueuse/firebase 附加组件中使用。

用法

js
import { useRTDB } from '@vueuse/firebase/useRTDB'
import { initializeApp } from 'firebase/app'
import { getDatabase } from 'firebase/database'

const app = initializeApp({ /* config */ })
const db = getDatabase(app)

// in setup()
const todos = useRTDB(db.ref('todos'))

您可以通过传递 autoDispose: false 来重用 db 引用

ts
const todos = useRTDB(db.ref('todos'), { autoDispose: false })

或者从核心包中使用 createGlobalState

js
// store.js
import { createGlobalState } from '@vueuse/core'
import { useRTDB } from '@vueuse/firebase/useRTDB'

export const useTodos = createGlobalState(
  () => useRTDB(db.ref('todos')),
)
js
// app.js
import { useTodos } from './store'

const todos = useTodos()

类型声明

typescript
export interface UseRTDBOptions {
  errorHandler?: (err: Error) => void
  autoDispose?: boolean
}
/**
 * Reactive Firebase Realtime Database binding.
 *
 * @see https://vueuse.org.cn/useRTDB
 */
export declare function useRTDB<T = any>(
  docRef: DatabaseReference,
  options?: UseRTDBOptions,
): Ref<T | undefined, T | undefined>

源代码

源码文档

贡献者

Anthony Fu
Robert Soriano
Antério Vieira
Anthony Fu
Jelf
Phil Li

更新日志

v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.3.0 on 7/30/2023
b5e52 - feat: add errorHandler option (#3232)

在 MIT 许可证下发布。