Javascript 在Promise catch块中访问此

Javascript 在Promise catch块中访问此,javascript,reactjs,react-native,promise,es6-promise,Javascript,Reactjs,React Native,Promise,Es6 Promise,在react native中,我如何在Promise catch block中使用“this” this.guestErrorAlert(); 在 问题与范围有关,请使用胖箭头解决问题 将此代码块替换为: .catch((error) => { GuestApi.onError(); console.log(error); this.guestErrorAlert(); }); 问题与范围有关,请使用胖箭头解决问题 将此代码块替换为: .catch((error

在react native中,我如何在Promise catch block中使用“this”

this.guestErrorAlert();


问题与范围有关,请使用胖箭头解决问题

将此代码块替换为:

.catch((error) => {
    GuestApi.onError();
    console.log(error);
    this.guestErrorAlert();
});

问题与范围有关,请使用胖箭头解决问题

将此代码块替换为:

.catch((error) => {
    GuestApi.onError();
    console.log(error);
    this.guestErrorAlert();
});

正如注释中提到的,您可以使用arrow函数,也可以定义函数并绑定它

例如:

let catchCallback = function() {...};
catchCallback = catchCallback.bind(this);

GuestApi.guestLogin()
      .then(...)
      .catch(catchCallback);

希望这有帮助

如注释中所述,您可以使用arrow函数,也可以定义函数并绑定它

例如:

let catchCallback = function() {...};
catchCallback = catchCallback.bind(this);

GuestApi.guestLogin()
      .then(...)
      .catch(catchCallback);

希望这有帮助

函数
替换为
箭头函数
-
()=>{}
请检查最后一个答案:我将尝试检查它是否在catch块中工作,并在此处报告结果匹配(函数(错误){})。绑定(此)不工作的GuestorAlert=>不工作将
函数
替换为
箭头函数
-
()=>{}
请检查最后一个答案:我将尝试检查它是否在catch块中工作,并在这里报告结果匹配(函数(错误){})。bind(此)not workingguestroralert=()=>not workingnot working bind或arrows@amorenew你做得对吗?我在上面看到了您的评论:
catch(函数(错误){}).bind(这个)不起作用
但是在这里,您根据catch()的结果绑定它,而不是在catch中传递的回调上绑定它。。。根据我的经验,bind工作得非常好,我经常在React中使用它。使用Alert而不导入它是我的错很高兴你找到了它;)祝您有个美好的一天!我也祝你愉快——:)不工作或者arrows@amorenew你做得对吗?我在上面看到了您的评论:
catch(函数(错误){}).bind(这个)不起作用
但是在这里,您根据catch()的结果绑定它,而不是在catch中传递的回调上绑定它。。。根据我的经验,bind工作得非常好,我经常在React中使用它。使用Alert而不导入它是我的错很高兴你找到了它;)祝您有个美好的一天!对你也很好——:)