2017-08-07 73 views
1

我正在嘗試根據URL的ID來動態設置Firebase參考。我正在使用Vue和VueFire。這裏是我的代碼:來自路徑ID的Firebase參考號

<template> 
    <v-container> 
    <h4>name: {{anObject.name}}</h4> 
    </v-container> 
</template> 

<script> 
    import Vue from 'vue' 
    import db from '@/js/firebase.js' 

    export default { 
    props: ['id'], 
    firebase: { 
     anObject: { 
     source: db.ref(this.getPath), 
     //source: db.ref('path/id'), 
     asObject: true, 
     cancelCallback: function() {console.log("Cancel")}, 
     readyCallback: function() {console.log("Ready")} 
    }, 
    computed: { 
    getPath() { 
     return 'path/' + this.id 
    } 
    } 
</script> 

目前的「anObject」對象不產生任何數據(沒有錯誤引發)。當我硬編碼路徑(當前顯示註釋掉),一切都按預期工作。我猜這個問題與'anObject'創建時沒有值的id屬性有關?如果是的話,我可以更新生命週期鉤子中的引用嗎?

回答

2

好的我找到了答案here。基本上我需要等待創建的鉤子,然後定義引用(注意不要在其他地方定義它!)。

created() { 
    this.$bindAsObject('anObject', db.ref('path/' + this.$route.params.id)) 
    },