【发布时间】:2018-11-04 18:32:46
【问题描述】:
我在访问函数内的数据属性时遇到问题。我错过了一些东西,我不知道是什么。
这是我的课
export default {
name: "Contact",
components: {
FooterComponent: FooterComponent,
NavigationComponent: NavigationComponent
},
data() {
return {
locale: Cookie.get('locale'),
nameAndLastName: '',
email: '',
subject: '',
message: '',
showPopUp: false
}
},
methods: {
sendEmail(e) {
e.preventDefault();
this.$validator.validateAll();
if (!this.$validator.errors.any()) {
let params = new URLSearchParams();
params.append('nameAndLastName', this.nameAndLastName);
params.append('email', this.email);
params.append('subject', this.subject);
params.append('message', this.message);
axios.post(this.$apiUrl + `rest/api/public/Contact/contact`, params, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(function (response) {
if (response.statusText === 'OK') {
console.log(this.showPopUp);
this.showPopUp = true;
}
})
.catch(function (error) {
console.log(error);
// This throws error TypeError: Cannot read property 'showPopUp' of undefined
});
}
}
},
mounted: function () {
console.log('test');
console.log(this.showPopUp);
},
}
所以问题是当我发送消息时,响应正常,发送电子邮件,但是当我尝试在挂载的钩子中打印 console.log(this.showPopUp) 时,我一直收到错误TypeError: Cannot read property 'showPopUp' of undefined... ,变量显示正常,为什么我不能从方法中访问它?我正在使用 VueJs 2。
如果您需要任何其他信息,请告诉我,我会提供。谢谢!
【问题讨论】:
标签: javascript vue.js vuejs2