登录页面为空判定的另一种方法

173 阅读1分钟

登录页面中非空校验,我没见过的方法:

      // 定义一个变量,使用object.key方法,some方法吧login中的值取反定义不能为空
      let hasError = Object.keys(this.login).some(key => !this.login[key]);
      hasError && Utils.error(this.$Message, '用户名或密码不能为空!');
      // 把hasError或闭包中的方法,用some来标识,判断两者有一个就为空,就提示
      hasError ||
        (() => {
          // 把下面这些方法放在闭包中使用
          this.loading = true;
          this.swtch = true;
          Request.Management.checkuserlogin(this.login).then(resp => {
            // console.log(resp,247);
            // console.log(resp.Data,147);
            if (resp.Data.Success) {
              // this.$store.dispatch('setTokeninfo',resp.Data.Token)
              sessionStorage.setItem('token', resp.Data.Token);
              sessionStorage.setItem('level', resp.Data.Level);
              this.$router.push('/');
              console.log(sessionStorage.setItem);
            } else {
              this.$Message('登录失败!');
            }
            this.loading = false;
            this.swtch = false;
            console.log(this.swtch, 47);
          });
        })();
    }
  }

这种方法是我之前没用过的,使用some方法定义对象中的key来实现判断