我第一次使用 VueUse 創建一個 Vue 應用程式。
在我的 main.ts 中,我有這個:
const firebaseConfig = {
//....
}
// Initialize Firebase
const app = initializeApp(firebaseConfig)
const db = getFirestore(app)
export const useTodos= createGlobalState(
() => useFirestore(collection(db, 'todos')),
)
所以在我看來,當我想使用我的 Todos 時,它可以很好地與 useTodos 掛鉤,但是有很多時候我需要訪問一個非常具體的檔案(比如/collection/key-one/sub-collections/key-two/property)。
通常,我會訪問這個:
const someDocument = useFirestore(doc(db, 'collection', myKey, 'sub-collections', myKeyTwo))
console.log(someDocument.property)
但我無權進入該db領域。我怎樣才能找回它?
uj5u.com熱心網友回復:
如評論中所述,并假設您使用的是 Firebase V9 或更高版本,
在你的 main.ts
export const db = getFirestore(app)
在其他視圖中:
import { db } from "../path/to/firebase.js"
const someDocumentRef = doc(db, `collection/${id}/sub-collection/${id}`);
const someDocumentSnapshot = await getDoc(someDocumentRef);
const myDoc = someDocument.data();
const myProperty = myDoc.someProperty;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/505217.html
標籤:Google Cloud Collective javascript 火力基地 Vue.js 谷歌云火库 vueuse
