首页 文章

未捕获(在承诺中)TypeError:无法读取属性'email'未定义[重复]

提问于
浏览
-1

这个问题在这里已有答案:

我正在使用带有laravel的vuejs应用程序,但我无法从对象中获取该组件应该怎么做

<script>
import axios from 'axios';
export default {
    data : function (){
        return {
            email: '',
            password: '',
            remember: 'true',
            errors: [],
        }
    },
    methods : {
        validateemail(mail){
            var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
            return re.test(mail);
        },
        attemptlogin(){
            this.errorsarray = [];
            axios.post('/login', {
                email: this.email,
                password: this.password,
                remember : this.remember
            }).then(function (response) {
                    location.reload();
                }).catch(function (error) {
                var code = error.response.status;
                console.log(this.email);
                    if(code == 422){


                        this.errors.push("Incorrect information");

                    }else{
                        console.log('no');
                        this.errors.push('Internal server error please try again later')
                    }
                }).then(function () {
                console.log('here');
            });
        },
    },
    computed : {
        isValidInput() {

            return this.validateemail(this.email) && this.password;
        }
    }
}

它也会显示这种错误

未捕获(承诺)TypeError:无法读取未定义的属性“email”

我不知道为什么有帮助?提前致谢

1 回答

  • 1

    使用 () => {} 函数语法而不是 function() {} . 前者将正确绑定 this .

    .catch(error => {
        console.log(this.email);
    })
    

    如果要使用 function() {} ,则必须显式绑定 this

    .catch(function (error) {
        console.log(this.email);
    }.bind(this))
    

    有关 bind 的更多信息,请参见this answer .

相关问题