与这个问题有关但是,在本地创建变量的解决方案对我来说不起作用。
我的情况是:
handleSubmit() {
var posting = $.post(this.props.url, {...});
posting.done(function() {
window.location.href = `/frontend/${this.props.value}`;
});
}
由于回调函数不能访问this
,我该如何将this
传递到回调中?如果我不打算使用Ajax。
三种方式
旧派-想想IE9之前的旧IE
var _this = this;
posting.done(function() {
window.location.href = `/frontend/${_this.props.value}`;
});
中学-从IE9到ie11
posting.done(function() {
window.location.href = `/frontend/${this.props.value}`;
}.bind(this));
New School -任何真正的浏览器
posting.done(() => {
window.location.href = `/frontend/${this.props.value}`;
});
考虑到您正在使用模板字面量,您可以安全地使用"new school";方法