7

我正在使用Vue 验证

我的 vuevlidate 中有以下内容

      validations: {
        user_form: {
            email: {required,email, isUnique(value) {
                    // standalone validator ideally should not assume a field is required
                    if (value === '') return true;
                    // simulate async call, fail for all logins with even length
                    return new Promise((resolve, reject) => {
                       this.$http.post("v1/user-management/users/email-registeredi",{email:value}).then((res)=>{
                            console.log("res is ", res);
                            resolve(true);
                        },(err)=>{
                            reject(false)
                        })
                    })
                }},
            role: {required},
            password: {required}
        }
    },

上面创建了一个无限循环的http请求,尤其是当它出现错误时

我哪里错了

4

2 回答 2

0

你不需要使用“new Promise”,因为 vue-resource 已经这样做了。尝试这个:

validations: {
    user_form: {
        email: {required,email, isUnique(value) {
                // standalone validator ideally should not assume a field is required
                if (value === '') return true;
                // simulate async call, fail for all logins with even length

               return this.$http.post("v1/user-management/users/email-registeredi",{email:value}).then((res)=>{
                    console.log("res is ", res);
                    return true;
                },(err)=>{
                    return false;
                });
            }},
        role: {required},
        password: {required}
    }
},
于 2018-09-13T12:33:39.013 回答
0

如果 vue validate 没有很好地处理拒绝承诺并创建无限循环。
您可以尝试,async await对于 Vue 验证的isUnique和 try and catch 返回false错误,
类似这样。

validations: {
 user_form: {
  email: {
    required,
    email,
    async isUnique (value) {
        if (value === '') return true
        try{
          const response = await this.$http.post("v1/user-management/users/email-registeredi",{email:value})
          return true;
        }
        catch(e){
          return false;
        }   
      }
   }
 }
于 2018-09-06T13:20:51.193 回答