【问题标题】:Ionic 2 proper way to use EventsIonic 2 正确使用事件的方法
【发布时间】:2017-01-18 20:37:59
【问题描述】:

我的应用程序中有身份验证提供程序 - AuthHelperProvider。当用户尝试登录失败时,应禁用登录按钮。为此,我在提供程序中发布事件并订阅 LoginPage 组件:

AuthHelperProvider

 this.http.get(query).map(res => <{error: string, clientId: string}>res.json()).subscribe(response => {
      if (response.error == '') {
        console.log(this.TAG + 'log in without errors');

        if (keepSingIn) {
          this.storage.set(this.KEEP_LOGGED_IN, true);
          this.setUsername(login);
          this.setPassword(password);
        }

        this.setClientId(response.clientId);
        this.events.publish('user:login');
        this.attemptCount = 0;
        /*this.navCtrl.setRoot(TabsPage);*/
      } else {
        console.log(this.TAG + 'log in with errors' + response.error);
        this.events.publish('user:failedAttempt',++this.attemptCount);
        this.errorHelper.showErrorAlert(response.error);
      }
    });

登录页面

 this.events.subscribe('user:failedAttempt', (attemptsCount) => {
      console.log(this.TAG + 'number of failed attempts: ' + attemptsCount);
      if (attemptsCount >= 3) {
        this.isLoginBlocked = true;
        this.errorAlert.showErrorToast('Number of attempts exceeded, try to restore your account');
      } else {
        this.isLoginBlocked = false;
      }
    });

但我遇到了意想不到的行为:订阅事件的处理程序称为 attemptsCount 次。看起来很傻,但我做错了什么?

【问题讨论】:

  • 您好,我刚刚删除了我的错误答案,您可能应该将您的发现添加到答案中并在几天后接受它,这样该线程就完成了问答。

标签: angular ionic-framework ionic2


【解决方案1】:

我发现问题在于我对 events.subscribe() 方法的返回类型的误解。因此,返回的参数数组应该如下所示:

   this.events.subscribe('user:failedAttempt', (attemptsCount) => {
      console.log(this.TAG + 'number of failed attempts: ' + attemptsCount[0]);
      if (attemptsCount[0] >= 3) {
        this.isLoginBlocked = true;
        this.errorAlert.showErrorToast('Number of attempts exceeded, try to restore your account');
      } else {
        this.isLoginBlocked = false;
      }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-25
    • 2017-02-18
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    相关资源
    最近更新 更多