在我的组件中,我正在做:
firebase() {
const userId = firebase.auth().currentUser.uid
return {
race: userRef.child(userId).child('races').child(this.raceKey)
}
},
mounted () {
console.log(this.$firebaseRefs.race.name)
}
我可以在组件模板内访问 race 属性的值,但我无法弄清楚如何在创建的 Hook 或方法内访问它们。该值始终是未定义的。我怎样才能做到这一点?
比赛的结构是:
race: {
name: "the name",
.....
}
最佳答案
这是因为对 Firebase 实时数据库的查询是异步的,因此无法保证您能在 mounted
等生命周期 Hook 中获得查询结果。换句话说,您的 race
对象的 Firebase 绑定(bind)在实例安装之前尚未完成。
有关更多详细信息和可能的解决方法,请参阅以下帖子 readyCallback :
https://github.com/vuejs/vuefire/issues/70和 https://github.com/vuejs/vuefire/issues/69
关于javascript - 如何在方法(Vuefire)中访问 firebase prop 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52409279/