【发布时间】:2020-12-05 11:01:28
【问题描述】:
我正在使用Laravel 和Vuejs 做一个小项目,一切都很好,我喜欢这两个框架。当我尝试使用 jQuery $.each 访问 goToStep 函数时,我遇到了一个小错误。
顺便说一句,jQuery 已经包含在内并且可以正常工作。唯一的问题是我无法在$.each 中访问goToStep。
错误信息:
Uncaught (in promise) TypeError: this.goToStep is not a function
我的 VuejS 代码:
store: function () {
axios.post("/apartment/", $("form#add-apartment").serialize()).then((response) => {
this.buildings = response.data.buildings;
alert(response.message)
}).catch(error => {
//console.log(error.response.data.errors)
$.each(error.response.data.errors, function(key, value){
var x = $("[name='"+ key +"']").closest("#parent").data('step');
this.goToStep(x)
});
});
},
goToStep: function (value) {
if (!this.validate()) {
return;
}
this.current_step = value;
},
【问题讨论】:
-
将
function(key, value) {更改为(key, value) => {以封装this上下文围绕$.each。更多信息here -
非常感谢您,已修复
标签: javascript jquery vue.js