简体   繁体   English

如何解决vue 3中的错误“ this is undefined”

[英]how to solve error “this is undefine” in vue 3

I have create a vue component like bellow: 我已经创建了一个像下面这样的vue组件:

<template>
    <div class="login">
        <h3>Sign in</h3>
        <input type="text" placeholder="Email" v-model="email"/><br>
        <input type="password" placeholder="Password" v-model="password"/><br>
        <button v-on:click="login()">Login</button>
    </div>
</template>

<script>
    import firebase from 'firebase'

    export default {
        name: "Login",
        data: function () {
            return {
                email: '',
                password: ''
            }
        },
        methods: {
            login: function () {
                firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(function (user) {
                    this.$router.replace('home')
                })
            }
        }
    }
</script>

It's still ok until I enter username and password and click Login, firebase auth success and console show error this is undefined. 直到我输入用户名和密码并单击“登录”,“ firebase身份验证成功”和“控制台显示错误”(这是未定义的),它仍然可以。 How can I solve this error? 我该如何解决这个错误? I cannot use router. 我不能使用路由器。

Try with an arrow function instead: 尝试使用箭头功能:

firebase.auth().signInWithEmailAndPassword(this.email, this.password).then((user) => {
    this.$router.replace('home')
})
login: function () {
   let vm=this
   firebase.auth().signInWithEmailAndPassword(this.email,  this.password).then(function (user) {
                    vm.$router.replace('home')
                })
            }

The callback function with the then does not have the context of login function available to it. 与回调函数then没有的情况下login的功能提供给它。 If you're able to use ES6, use an arrow function instead : 如果您可以使用ES6,请改用箭头功能:

 login: function () {
            firebase.auth().signInWithEmailAndPassword(this.email, this.password).then((user) => {
                this.$router.replace('home')
            })
        }

However, if you for some reason are not able to use ES6, try storing a reference to this and use it inside the function : 但是,如果由于某种原因您无法使用ES6,请尝试存储this的引用并在函数中使用它:

 login: function () {
           var self = this;
            firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(function (user) {
                self.$router.replace('home')
            })
        }

Using arrow function or bind() method Example: 使用箭头函数或bind()方法示例:

foo().then(() => {
     // access this
})
// or
foo().then(function() {
     // access this
}.bind(this))

暂无
暂无

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

相关问题 不推荐使用错误过滤器如何解决 vue.js 3 中的此错误 - error Filters are deprecated how to solve this error in vue.js 3 导轨5未定义模板错误 - rails 5 undefine template error 如何解决 Vue-Formulate Failed to resolve component 错误 - How to solve Vue-Formulate Failed to resolve component error 如何使用 javascript 构造函数,它显示未定义错误 - How do I use javascript constructor, its showing undefine error 上载图片时如何解决jQuery ajax post方法未定义索引问题? - How to solve this jQuery ajax post method undefine index problem while uploading images? 如何解决:[Vue警告]:在vue.js 2上渲染组件时出错? - How to solve : [Vue warn]: Error when rendering component on the vue.js 2? 我该如何解决 [Vue 警告]:数据()中的错误:Laravel Vue 中的“ReferenceError:total is not defined” - How can i solve [Vue warn]: Error in data(): “ReferenceError: total is not defined” in Laravel Vue 我在iframe中得到一个未定义的错误 - I get an undefine error in iframe 我如何从书路中解决问题以在第 78 页做出反应? 对于表格组件中的一种方法,我一直在取消定义 - How can i solve the problem from the book road to react at page 78 ? I keep getting undefine for one of the methods in the table component Vue.js - 如何解决 [Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性‘端口’”? - Vue.js - How can I solve [Vue warn]: Error in render: "TypeError: Cannot read property 'port' of undefined"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM