跳到主要內容

useFirestore

類別
導出大小
653 B
套件
@vueuse/firebase
上次變更
5 天前

響應式 Firestore 綁定。使其簡單明瞭地始終保持您的本地資料與遠端資料庫同步。可在 @vueuse/firebase 附加元件中使用。

用法

js
import { useFirestore } from '@vueuse/firebase/useFirestore'
import { initializeApp } from 'firebase/app'
import { collection, doc, getFirestore, limit, orderBy, query } from 'firebase/firestore'
import { computed, shallowRef } from 'vue'

const app = initializeApp({ projectId: 'MY PROJECT ID' })
const db = getFirestore(app)

const todos = useFirestore(collection(db, 'todos'))

// or for doc reference
const user = useFirestore(doc(db, 'users', 'my-user-id'))

// you can also use ref value for reactive query
const postsLimit = shallowRef(10)
const postsQuery = computed(() => query(collection(db, 'posts'), orderBy('createdAt', 'desc'), limit(postsLimit.value)))
const posts = useFirestore(postsQuery)

// you can use the boolean value to tell a query when it is ready to run
// when it gets falsy value, return the initial value
const userId = shallowRef('')
const userQuery = computed(() => userId.value && doc(db, 'users', userId.value))
const userData = useFirestore(userQuery, null)

跨實例共享

您可以通過傳遞 autoDispose: false 來重複使用 db 參考。您還可以設置自動處置 db 參考之前的毫秒數。

注意:再次獲取未處置的 db 參考不會消耗 Firestore 讀取次數。

ts
const todos = useFirestore(collection(db, 'todos'), undefined, { autoDispose: false })

或使用核心套件中的 createGlobalState

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

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

export default {
  setup() {
    const todos = useTodos()
    return { todos }
  },
}

類型宣告

typescript
export interface UseFirestoreOptions {
  errorHandler?: (err: Error) => void
  autoDispose?: boolean | number
}
export type FirebaseDocRef<T> = Query<T> | DocumentReference<T>
type Falsy = false | 0 | "" | null | undefined
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<DocumentReference<T> | Falsy>,
  initialValue: T,
  options?: UseFirestoreOptions,
): Ref<T | null>
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<Query<T> | Falsy>,
  initialValue: T[],
  options?: UseFirestoreOptions,
): Ref<T[]>
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<DocumentReference<T> | Falsy>,
  initialValue?: T | undefined | null,
  options?: UseFirestoreOptions,
): Ref<T | undefined | null>
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<Query<T> | Falsy>,
  initialValue?: T[],
  options?: UseFirestoreOptions,
): Ref<T[] | undefined>

原始碼

原始碼文檔

貢獻者

Anthony Fu
Anthony Fu
Kiyohiko Heima
IlyaL
Zehir
Antério Vieira
azaleta
sun0day
Alex Kozack
Rodolfo Román
Matthias Stiller
Phil Li

更新日誌

v12.8.0 於 2025/3/5
7432f - feat(types): 棄用 MaybeRefMaybeRefOrGetter,改用 Vue 的原生類型 (#4636)
v12.0.0-beta.1 於 2024/11/21
0a9ed - feat!: 移除 Vue 2 支援,優化捆綁包並清理 (#4349)
v10.0.0-beta.3 於 2023/4/12
05781 - feat: 支援 autoDispose 的延遲,修復 #2252 (#2276)

根據 MIT 許可證發布。