如何在vue.js中将用户信息提供给应用程序的“数据”部分?

时间:2018-04-10 18:17:03

标签: firebase vue.js vuefire

您是否恰好知道如何将用户设置为currentUser? 我正在使用vuefire和firebase。 user.uid 返回undefined。

auc_precision_recall

这是我的其余代码:

 <td v-if="(building['key'] && building['key'].child('ownerId')) == user.uid">
            <p >{{building['.key']}} + {{user.uid}}</p>
</td>

我试图通过beforeCreate hook中的商店来实现:

import firebase,{ db, usersRef, buildingsRef } from '../firebase-config';
import { store } from '../store/store';

export default {

  firebase() { 
    // from my understanding this just gives me a quick access to the Refs and a short nomenclature
    return { 
    users: usersRef,  
    buildings: buildingsRef, 
    }
  },
  name: 'buildings',
  data () {
    return {
      user: "",
      building: {
        name: '',
        address: '',
        comments: '',
        ownerId: '',
      },
    }
  },

如果我在create hook中设置用户,就像这样:

  beforeCreate () {
       this.$store.dispatch('setUser');
       let user = this.$store.getters.getUser;
       console.log(user); // this works!!!
      this.$set(this.user, 'uid', user.uid )
  }   
},

我收到此错误:

enter image description here

1 个答案:

答案 0 :(得分:2)

在Vue中,状态(又名data属性)是initialized between the beforeCreate and created个钩子。

因此,您对data beforeCreate所做的任何更改丢失

将您的挂钩更改为created

created() {                         // changed this line
  this.$store.dispatch('setUser');
  let user = this.$store.getters.getUser;
  console.log(user); // this works!!!
  this.$set(this.user, 'uid', user.uid)
}