简体   繁体   中英

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

I am import a firebase at the top of my /buildings.vue component:

...
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:

...
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 . Export it like below.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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