跳到主要内容

useRTDB

分类
导出大小
257 B
@vueuse/firebase
上次更改
8 个月前

响应式 Firebase Realtime Database 绑定。让本地数据始终与远程数据库保持同步变得简单明了。可在 @vueuse/firebase 插件中使用。

用法

ts
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 来复用数据库引用

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

或者使用核心包中的 createGlobalState

ts
// store.ts
import { 
createGlobalState
} from '@vueuse/core'
import {
useRTDB
} from '@vueuse/firebase/useRTDB'
export const
useTodos
=
createGlobalState
(
() =>
useRTDB
(db.ref('todos')),
)
vue
<!-- app.vue -->
<script setup lang="ts">
import { 
useTodos
} from './store'
const
todos
=
useTodos
()
</script>

类型声明

ts
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
SerKo
Anthony Fu
Jelf
Phil Li

更新日志

0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v10.3.0
b5e52 - feat: 添加 errorHandler 选项 (#3232)

根据 MIT 许可证发布。