1

你会碰巧知道如何将用户设置为当前用户吗?我正在使用 vuefire 和 firebase。user.uid返回未定义。

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

这是我的其余代码:

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: '',
      },
    }
  },

我正在尝试通过 beforeCreate 钩子中的商店来做到这一点:

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

如果相反,我将用户设置在创建钩子中,如下所示:

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

我收到此错误:

在此处输入图像描述

4

1 回答 1

2

在 Vue 中,状态(又名data属性)在钩子之间初始化。beforeCreatecreated

因此,您对datain所做的任何更改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)
}
于 2018-04-10T18:30:50.277 回答