useRTDB
響應式 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.dev.org.tw/useRTDB
*/
export declare function useRTDB<T = any>(
docRef: DatabaseReference,
options?: UseRTDBOptions,
): Ref<T | undefined, T | undefined>