我的代码是:
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
var user1 = firebase.auth().currentUser;
var name, email, photoUrl, password, uid, emailVerified;
if (user != null) {
name = user1.displayName;
email = user1.email;
photoUrl = user1.photoURL;
emailVerified = user1.emailVerified;
uid = user1.uid; // The user's ID, unique to the Firebase project. Do NOT use
// this value to authenticate with your backend server, if
// you have one. Use User.getToken() instead.
console.log(email);
}
} else {
console.log('no user');
}
});
console.log(email)打印我的电子邮件但是在html中
<h1>email: {{ email }}</h1>
不要打印任何东西,为什么?我用谷歌搜索但没有。怎么了?
答案 0 :(得分:3)
要在html中打印,变量应该是类属性。
在您的组件类中,声明email
。
@Component()
export class cmp{
email:any;
//...
//In your firebase call set in this.email
firebase.auth().onAuthStateChanged((user)=> {//use arrow function to make sure this is correct
if (user) {
let user1 = firebase.auth().currentUser;
let name, photoUrl, password, uid, emailVerified;
if (user != null) {
name = user1.displayName;
this.email = user1.email;//here
photoUrl = user1.photoURL;
emailVerified = user1.emailVerified;
uid = user1.uid; // The user's ID, unique to the Firebase project. Do NOT use
// this value to authenticate with your backend server, if
// you have one. Use User.getToken() instead.
console.log(this.email);
}
} else {
console.log('no user');
}
});
现在,在html中显示
{{email}}