如何从另一个组件内的方法导入firebase引用?

时间:2018-04-10 14:24:06

标签: javascript firebase vue.js components vuefire

我在 /buildings.vue 组件的顶部导入了一个firebase:

...
import buildingsadd, { buildingsRef } from './buildingsadd.vue';


export default {
  firebase() { 
    return { 
    buildings: buildingsRef
    }
  },
  components: {
    buildingsadd
  },
  name: 'buildings',
  ...

/buildingsadd.vue 组件上,我正在定义firebase ref的新路径,如下所示:

...
import firebase from '../firebase-config';
import { db } from '../firebase-config';
export default {
  firebase() { 
    return {
    buildings:  buildingsRef,
    users: usersRef,
    }
  },
  name: 'buildingsadd',
  data () {
    return {
      newBuilding: {
        name: '',
      } 
    }
  },
  methods: {
    addBuilding: function () {
      let userId = firebase.auth().currentUser.uid;
      let buildingsRef = db.ref('buildings/'+userId);
    }
...

但我收到了这个错误:

enter image description here 任何想法?

对此的回答也将解决我的特殊问题:

1 个答案:

答案 0 :(得分:0)

您没有导出buildingsRef。将其导出如下。

<强> buildingsadd.vue

import firebase from '../firebase-config';
import { db } from '../firebase-config';

let userId = firebase.auth().currentUser.uid;                  // added
export let buildingsRef = db.ref('buildings/'+userId);         // added

export default {
  // ... (all the same)
  methods: {
    addBuilding: function () {
                                                               // removed lines here
    }
...