<div class="login" v-show="activeTab === 1"> <div class="panel-content"> <div class="login-account-input panel-input"> <el-input v-model="loginData.account" placeholder="手機號/帳號/平臺號" clearable v-validate="'required'" data-vv-scope="login" name="account" data-vv-as="手機號/帳號/平臺號" ></el-input> </div> <div class="login-pwd-input panel-input"> <el-input v-model="loginData.pwd" placeholder="請輸入登陸密碼" clearable v-validate="'required'" data-vv-scope="login" name="pwd" data-vv-as="密碼" ></el-input> </div> </div> <div class="panel-errors"> <span v-if="errors.has('login.account')">{{errors.first('login.account')}}</span> <span v-else-if="errors.has('login.pwd')">{{errors.first('login.pwd')}}</span> <span v-else-if="otherError">{{otherError}}</span> </div> <div class="panel-btns"> <el-button class="default-btn" @click="loginDialogVisible = false">取消</el-button> <el-button class="default-btn" type="main" @click="login">登陸</el-button> </div> </div> <div class="register-panel" v-show="activeTab === 2"> <div class="panel-content"> <div class="register-mobile-input panel-input"> <el-input v-model="registerData.mobile" placeholder="請輸入手機號" clearable v-validate="'required'" data-vv-scope="register" name="mobile" data-vv-as="手機號" ></el-input> </div> <div class="register-code-input panel-input"> <el-input v-model="registerData.verCode" placeholder="請輸入驗證碼" clearable v-validate="'required'" data-vv-scope="register" name="code" data-vv-as="驗證碼" ></el-input> </div> <div class="register-pwd-input panel-input"> <el-input v-model="registerData.pwd" placeholder="請輸入密碼(6-18位數字與字母組合)" clearable v-validate="'required'" data-vv-scope="register" name="pwd" data-vv-as="密碼" ></el-input> </div> </div> <div class="panel-errors"> <span v-if="errors.has('register.mobile')">{{errors.first('register.mobile')}}</span> <span v-else-if="errors.has('register.code')">{{errors.first('register.code')}}</span> <span v-else-if="errors.has('register.pwd')">{{errors.first('register.pwd')}}</span> <span v-else-if="otherError">{{otherError}}</span> </div> <div class="panel-btns"> <el-button class="default-btn" @click="loginDialogVisible = false">取消</el-button> <el-button class="default-btn" @click="register" type="main">提交</el-button> </div> </div> methods: { tabHandler(index) { this.activeTab = index }, login() { this.$validator.validateAll('login').then(res => { console.log(res) }) }, register() { this.$validator.validateAll('register').then(res => { console.log(res) }) }, initLoginDialog() { this.loginData = { account: '', pwd: '' } this.registerData = { mobile: '', verCode: '', pwd: '' } setTimeout(() => { this.errors.clear('login') this.errors.clear('register') }, this.$resetMillisecond) this.otherError = '' } }, watch: { loginDialogVisible(val) { val && this.initLoginDialog() }
// 校驗全部字段. validator.validate(); // 驗證要匹配的字段. validator.validate('field'); // 校驗某個範圍下的字段 validator.validate('scope.field'); // 校驗某個範圍下的全部字段. validator.validate('scope.*'); // 校驗未配置範圍的字段. validator.validate('*');
參考:javascript
data-vv-scope 說明html
https://baianat.github.io/vee-validate/api/data-attrs.htmljava
Validate API 說明git
https://baianat.github.io/vee-validate/api/validator.html#apigithub
使用入門api
https://blog.csdn.net/qq_14988399/article/details/80678317ui
https://blog.csdn.net/beyond__devil/article/details/84956810this