参考地址:https://segmentfault.com/q/1010000003988864

github地址:https://github.com/fergaldoyle/vue-form

安装插件

cnpm install vue-form --save

使用,在main.js里加入

import VueForm from 'vue-form'

Vue.use(VueForm)

代码实战

  1 <template>
  2     <div class="login__content">
  3         <vue-form :state="formstate" v-model="formstate">
  4     <div class="login__title">SIGN IN</div>
  5     <div class="login__content_input username__margin_bottom">
  6            <validate auto-label class="form-group required-field" :class="fieldClassName(formstate.name)">
  7             <input type="text"  name='name'  placeholder="Username" required v-model="model.name"/>            
  8         </validate>
  9              <field-messages name="name" v-show="isSubmit">
 10            <!--<div class="success__msg"></div>-->
 11            <div class="error__msg" slot="required">Username is a required field</div>
 12              </field-messages>
 13     </div>
 14     <div class="login__content_input">
 15         <validate auto-label class="required-field" :class="fieldClassName(formstate.password)">
 16             <input type="password" name="password"  placeholder="Password" required v-model="model.password" />
 17         </validate>
 18         <field-messages name="password" v-show="isSubmit" >
 19              <!--<div class="success__msg"></div>-->
 20              <div class="error__msg" slot="required">Password is a required field</div>
 21         </field-messages>
 22         
 23     </div>
 24     <div class="error__msg" v-if='errorMsg["fail"]'>{{errorMsg["fail"]}}</div>
 25     <div class="login__in_part">
 26         <div class="forgot__password">
 27             <a >Forgot Password</a>
 28         </div>
 29         <div class="login__remember">
 30             <label><input type="checkbox" v-model="isRemember" /> Remember me</label>
 31         </div>
 32         <div class="login__in">
 33             <button  class="signin" @click.prevent="login"  >SIGN IN</button>
 34         </div>
 35     </div>
 36     <div class="sign__register">Don't have an account?  <a href="#" >Sign Up</a> </div>
 37     </vue-form>
 38 </div>
 39 </template>
 40 <script>
 41 import config from '../../utils/config.js'
 42 export default{
 43     data(){
 44         return{
 45             formstate: {},
 46             errorMsg:{},
 47             isSubmit:false,
 48             isRemember:true,
 49             model:{
 50                 name:'',
 51                 password:''
 52             }
 53         }
 54     },
 55     methods:{
 56         fieldClassName: function (field) {
 57       if(!field) {
 58         return '';
 59       }
 60       if((field.$touched || field.$submitted) && field.$valid && !this.errorMsg["fail"]) {
 61         return 'has-success';
 62       }
 63       if((field.$touched || field.$submitted) && field.$invalid && this.errorMsg["fail"]) {
 64         return 'has-danger';
 65       }
 66     },
 67     login(){
 68         this.isSubmit=true;
 69         if(this.formstate.$valid===false){
 70             return
 71         }
 72         let o={
 73             email:this.model.name,
 74             password:this.model.password
 75         }
 76         this.$axios.post(`${config.apiPreUrl}/login/getToken`, o).then((res)=>{
 77             console.log(res.data);
 78         })
 79     }
 80     }
 81 }
 82 </script>
 83 <style lang="scss" >
 84 .login__content{
 85   width: 452px;
 86   height: 495px;
 87   background: white;
 88   position: relative;
 89   box-shadow: 0px 0px 14px #423D40;
 90   padding: 0 44px 0 43px;
 91 }
 92 .login__title{
 93   padding: 56px 0 27px 0px;
 94   font-size: 18px;
 95   color: #3A3A3A;
 96   font-weight: bolder;
 97 }
 98 .login__content_input{
 99   width: 365px;
100   position: relative;
101   input{
102     height: 48px;
103     padding: 0 8px;
104     background-color: #ffffff;
105     padding-left: 15px;
106     width: 100%;
107     outline: none;
108     font-size: 15px;
109     border: solid 1px #C4C4C4;
110     &:hover{
111         border: solid #C4C4C4 1px;
112         box-shadow:0 0 3px rgba(136,136,136,0.7);
113     }
114   }
115 }
116 .username__margin_bottom{
117   margin-bottom: 25px;
118 }
119 .forgot__password{
120   font-size: 11px;
121   padding: 9px 0 0 4px ;
122   a{
123     color:#E25043;
124     cursor: pointer;
125   }
126 }
127 .login__remember{
128    padding: 29px 0;
129    font-size: 11px;
130    color: #707D95;
131    font-weight: lighter;
132    input{
133         margin-top:0px !important;
134         margin-right: 5px;
135         vertical-align: middle;
136    }
137 }
138 .login__in_part{
139   padding-left: 4px;
140 }
141 .login__in{
142   height: 44px;
143   button{
144     width: 50%;
145     height: 44px;
146     color: #000;
147     border: 0;
148     background: #E25043;
149     color: #ffffff;
150     font-size: 12px;
151   }
152 }
153 .sign__register{
154   padding: 29px 0 0 6px;
155   color: #4A4A4A;
156   font-size: 12px;
157   a{
158     color: #E25043;
159     text-decoration: none;
160   }
161 }
162 .error__msg{
163   color: #E25043;
164   font-size: 13px;
165   padding: 7px 0 0 0 ;
166 }
167 .has-danger{
168   border: solid 1px #EB7057!important;
169 }
170 .has-success{
171   border: solid 1px #43C7A9 !important;
172 }
173 .has-normal{
174   border:solid #C4C4C4 1px!important;
175 }
176 </style>

 

posted on 2018-01-22 17:31  执候  阅读(656)  评论(0编辑  收藏  举报