简体   繁体   English

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

[英]How to import firebase reference from a method inside another component?

I am import a firebase at the top of my /buildings.vue component: 我将Firebase导入到/buildings.vue组件的顶部:

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


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

On the the /buildingsadd.vue component I am defining a new path to the the firebase ref like this: /buildingsadd.vue组件上,我正在定义Firebase引用的新路径,如下所示:

...
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);
    }
...

But I am getting this errors: 但是我得到这个错误:

在此处输入图片说明 any ideas? 有任何想法吗?

Answers to this would also solve my particular problem: 答案也可以解决我的特殊问题:

You are not exporting buildingsRef . 您未在导出buildingsRef Export it like below. 如下导出。

buildingsadd.vue : 建筑add.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
    }
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM