简体   繁体   English

使用Vue.js从Firebase获取用户电子邮件

[英]Getting user email from firebase using Vue.js

I'm building a chat app with Vue.js and Firebase. 我正在使用Vue.js和Firebase构建一个聊天应用程序。

I'm new to both vue and firebase and struggeling to get the users email so i can send it to firebase to show along with the chat. 我对vue和firebase都是陌生的,我一直在努力获取用户电子邮件,因此我可以将其发送到firebase与聊天一起显示。

I've tried this solution: How can i get the user in firebase database, to write from a component with vuejs? 我已经尝试过以下解决方案: 我如何才能使用户进入Firebase数据库,以便使用vuejs从组件进行编写?

But can't get it to work. 但是无法使其正常工作。 I guess I dont really get where, how or when I can access root. 我想我真的不知道我在哪里,如何或何时可以访问root。 Cause when i tried this.$root.something I get an error message. 原因是当我尝试this。$ root.something时出现错误消息。

This code is in my main.js file: 这段代码在我的main.js文件中:

firebase.auth().onAuthStateChanged(function(user) {
    if (!app) { 
        /* eslint-disable no-new */
        app = new Vue({
          el: '#app',
          data: {email: user.email}, //here i want to store the email, which works but I cant access it from other components
          template: '<App/>',
          components: { App },
          router 
        })
    }
});

And this is the script in my main component. 这是我主要组件中的脚本。 It's here I want to accses the root. 我想在这里成为根。

<script>
    import * as firebase from 'firebase'

    export default {
        name: 'chat',
        data: function(){
            return {
                room: null,
                db: null, // assign Firebase SDK later
                messageInput:'', // this is for v-model
                messages: [],
            }
        },
        mounted() {
            this.db = firebase
            // access the location and initilize a Firebase reference
            this.init()
        },
        methods: {
            init(){
                this.room = this.db.database().ref().child('chatroom/1')
                this.messageListener()
                this.saveEmail();
            },
            saveEmail(){
//here i tried to save the email using the onAuthStateChanged method
                firebase.auth().onAuthStateChanged(function(user) {
                    this.$root.email = user.email;
                });
            },
            send(messageInput) {
                //A data entry.
                let data = {
                    message: messageInput
//here i want to add it to the database
                    // user: this.$root.email
                };
                // Get a key for a new message.
                let key = this.room.push().key;
                this.room.child('messages/' + key).set(data)
                // clean the message
                this.messageInput = ''
            },

            messageListener () {      
                this.room.child('messages').on('child_added', (snapshot) => {
                    // push the snapshot value into a data attribute
                    this.messages.push(snapshot.val())
                })
            },
            logout(){
                firebase.auth().signOut().then(() => {
                    this.$root.email = null;
                    this.$router.replace('login');
                })
            },  
        }
    }
</script>

And here is the script in my login component : 这是我的登录组件中的脚本:

<script>

    import firebase from 'firebase'

    export default {
        name: 'login',
        data: function(){
            return {
                email: '',
                password: '',
            }
        },
        methods: {
            signIn: function(){
                firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(
                    (user) => {
                        this.$root.email = user.email;
                        this.$router.replace('chat');
                    },
                    (err) => {
                        alert('Opppps! ' + err.message);
                    }
                );
            },
        }
    }

</script>

Sorry if I'm not being clear. 对不起,如果我不清楚。 Thanks in advance! 提前致谢!

The callback of the onAuthStateChanged method is bind to the wrong this scope. onAuthStateChanged方法的回调绑定到错误的此作用域。 You can easily fix this by using an arrow function like below. 您可以使用以下箭头功能轻松修复此问题。 When using an arrow function, it will automatically bind to the context it is defined in. 使用箭头功能时,它将自动绑定到定义它的上下文。

saveEmail() {
  firebase.auth().onAuthStateChanged((user) => {
    this.$root.email = user.email;
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM