我使用Firebase登录。我也在使用Vue.js和VueFire。如果我使用--all
调用,我不确定如何将onUserLogin的结果导入到vue应用程序的数据中。我尝试创建一个"全球"变量(称为firebase.auth().onAuthStateChanged( onUserLogin );
)然后将其映射到数据中,但不会在我的模板中显示email
。也许我需要在创建Vue应用程序时在我的{{ email }}
属性中设置onUserLogin?或者,我应该以某种方式在Firebase属性中使用methods
(这是否与登录自动关联?)
user
我的HTML看起来像这样(基本上只是一个调用var onUserLogin = function(user) {
if (user) {
email = user.email;
console.log( "email", email );
}
}
var email;
new Vue( {
el: "#app",
data: {
email: email
},
firebase: {} // Is something supposed to be here?
methods: {
loginToFirebase: function() {
if (!firebase.auth().currentUser) {
var provider = new firebase.auth.GithubAuthProvider();
firebase.auth().signInWithPopup( provider ).then( afterSignIn ).catch( signInError );
} else {
firebase.auth().signOut();
}
},
}
});
firebase.auth().onAuthStateChanged( onUserLogin );
的按钮):
loginToFirebase
答案 0 :(得分:3)
答案是将firebase.auth().onAuthStateChanged( onUserLogin );
放在挂钩中:
new Vue( {
mounted: function() {
firebase.auth().onAuthStateChanged( this.onUserLogin );
},
data: {
user: undefined
}
methods: {
onUserLogin: function( user ) {
this.user = user;
}
}
el: "#app"
})