发表于: 2019-11-01 22:58:42

1 1079


今天完成的事情:

今天有一个问题一直萦绕在我的心头,我上午在写登陆到后台的功能,就是一个if判断,昨天请求已经发出去了,今天开始写验证,如果密码正确就登陆,密码不正确就提示用户不存在,但是当我写好之后发现一直都是else,提示账号不存在,既然不存在肯定是第一个验证的方式写错了,我是这么写的:

s.$axios.post('/api/a/login',this.formData,
     // {headers: {'Content-Type': 'application/x-www-form-urlencoded'}},
     {transformRequest: function(obj) {
var str = [];
         for(let p in obj){
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
         }
return str.join("&");
       }
},
   ).then(
(response) => {
if (response.data.code == "0") {
console.log(response.data.message);
         this.$router.push('/Index');
         // this.$router.push({ path:'/backControl'});
       }else{
this.hint = response.data.message;
       }
})
}
}

第一行的thisformdata刚开始不行是因为没使用默认的字段,当我把

这个改为默认的字段,然后再用formdata问题就解决了。

vue爬坑——vee-validate的使用

1、npm安装vee-validate

npm install vee-validate --save

1

安装时要注意安装的版本,不同的版本使用的方式不一样,这里我安装的是”^2.0.0-rc.26”。

具体版本的使用见官网:vee-validate官网

2、main.js里引用vee-validate插件

import Vue from 'vue'

import VeeValidate,{ Validator } from 'vee-validate'

import zh_CN from 'vee-validate/dist/locale/zh_CN'

Vue.use(VeeValidate);

3、vee-validate提示语汉化

//提示语汉化

Validator.locale ==="en" ? "zh_CN" : "en";

Validator.localize(Validator.locale,{

    messages: zh_CN.messages,

    attributes:{

        loginName:'登录名',

        loginPassword:'密码'

    }

});


4、验证方法扩展

Validator.extend('phone', {

    getMessage: (field, [args]) => `请输入正确的手机号码`,

    validate: (value, [args]) =>{

        const reg = /^((13|14|15|17|18)[0-9]{1}\d{8})$/;

        return reg.test(value) 

    }  

});

5、组件中使用

<form class="form" autocomplete="off" @submit.prevent="formSubmit">

  <div class="form-item">

    <input type="text" placeholder="请输入手机号" v-model="loginName" name="loginName" id="loginName" v-validate.initial="'required|phone'" maxlength="11" :class="{'valid-error':errors.has('loginName')}"/>

  </div>

  <div class="form-item">

    <input type="password" placeholder="请输入密码" v-model="loginPassword" maxlength="18" name="loginPassword" id="loginPassword" :class="{'valid-error':errors.has('loginPassword')}" v-validate.initial="'required|password'" />

  </div>

  <div class="form-item clearfix">

    <router-link to="" class="login-link color-blue login-forget fr"><span class="iconfont icon-wenhao" replace></span>忘记密码</router-link>

  </div>

  <button class="form-btn bg-blue" type="submit">登录</button>

</form>


明天计划的事情:开始写后台。

收获:认识了vee-validate插件,但是还是太水了,明天继续努力。



返回列表 返回列表
评论

    分享到