本文介绍了如何将用户信息获取到“数据"列表中. vue.js中应用程序的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您是否会偶然知道如何将用户设置为currentUser?我正在使用vuefire和firebase. user.uid 返回未定义.
Would you happen to know how to set the user to the currentUser?I am using vuefire and firebase.The user.uid is returning undefined.
<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挂钩中的商店进行操作:
I am trying to do it through the store in the beforeCreate hook:
beforeCreate () {
this.$store.dispatch('setUser');
let user = this.$store.getters.getUser;
console.log(user); // this works!!!
this.$set(this.user, 'uid', user.uid )
}
},
如果相反,我在创建钩子中设置用户,如下所示:
If instead I set the user in create hook like this:
created () {
this.$store.dispatch('setUser');
let user = this.$store.getters.getUser;
console.log(user); // this works!!!
this.$set(this.user, 'uid', user.uid )
}
我收到此错误:
推荐答案
在Vue中,状态(又称为data
属性)为在beforeCreate
和created
钩子之间初始化.
In Vue, the state (aka data
properties) is initialized between the beforeCreate
and created
hooks.
因此,您对beforeCreate
中的data
所做的任何更改都会丢失.
So any change you do to data
in beforeCreate
is lost.
将钩子更改为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)
}
这篇关于如何将用户信息获取到“数据"列表中. vue.js中应用程序的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!