javascript - 未捕获(承诺)类型错误:无法读取未定义的属性“电子邮件”
问题描述
我正在使用带有 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;
}
}
}
它也显示这种错误
未捕获(承诺)类型错误:无法读取未定义的属性“电子邮件”
我不知道为什么有任何帮助?提前致谢
解决方案
使用() => {}
函数语法而不是function() {}
. 前者将this
正确绑定。
.catch(error => {
console.log(this.email);
})
如果要使用,function() {}
则必须显式绑定this
:
.catch(function (error) {
console.log(this.email);
}.bind(this))
有关更多信息,请参阅此答案bind
。
推荐阅读
- javascript - 为什么我不能从这个回调函数内部调用一个类方法?错误类型错误:这是未定义的
- python - 使用烧瓶将数据插入 postgresql 时,如何解决 UniqueConstraint 错误?
- javascript - 从 $scope 访问嵌套变量属性的干净方法
- node.js - 为什么“res.sendFile()”函数(Express.js)不能与“pug”文件一起使用?
- java - CompletableFuture 的完成顺序
- coordinates - 从一个 2D 坐标系转换为具有不同 (0,0) 的另一个坐标系
- python - Pandas:前向填充与包含列表对象的系列重叠
- c# - 如何将具有索引的数组映射到具有自定义 JsonConverter 的对象
- java - 如何优化返回列表以进行调试
- angular - 离子角度中的常见错误验证消息