代码之家  ›  专栏  ›  技术社区  ›  amorenew

在Promise catch块[重复]中访问此

  •  0
  • amorenew  · 技术社区  · 7 年前

    this.guestErrorAlert();
    

     GuestApi
          .guestLogin()
          .then(function (response) {
            GuestApi.onSuccess(response);
            return response;
          })
          .then(() => this.openLanguageStartupScreen())
          .catch(function (error) {
            GuestApi.onError();
            console.log(error);
            this.guestErrorAlert();
          });
    
    2 回复  |  直到 7 年前
        1
  •  3
  •   Vivek Doshi    7 年前

    问题在于范围,使用胖箭头解决问题。

    .catch((error) => {
        GuestApi.onError();
        console.log(error);
        this.guestErrorAlert();
    });
    
        2
  •  0
  •   sjahan    7 年前

    如注释中所述,您可以使用箭头函数,也可以定义函数并将其绑定。

    例子:

    let catchCallback = function() {...};
    catchCallback = catchCallback.bind(this);
    
    GuestApi.guestLogin()
          .then(...)
          .catch(catchCallback);
    

    希望这有帮助!