亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue項(xiàng)目的表單校驗(yàn)實(shí)戰(zhàn)指南

 更新時(shí)間:2024年10月21日 10:21:33   作者:巴啦啦不亮w  
這篇文章主要介紹了Vue項(xiàng)目表單校驗(yàn)的相關(guān)資料,前端表單校驗(yàn)?zāi)軠p少無(wú)效請(qǐng)求,保護(hù)后端接口,使用ElementPlus表單組件進(jìn)行校驗(yàn),需要準(zhǔn)備表單對(duì)象、規(guī)則對(duì)象并進(jìn)行雙向綁定,用戶名、密碼以及協(xié)議勾選等字段都需符合特定規(guī)則,需要的朋友可以參考下

一、表單檢驗(yàn)

1.作用:前端校驗(yàn)可以省去一些錯(cuò)誤的請(qǐng)求提交,為后端節(jié)省接口壓力

2.流程:

表單數(shù)據(jù)   =》  前端校驗(yàn)(過(guò)濾錯(cuò)誤請(qǐng)求)  =》   后端查詢是否匹配

3.工具:ElementPlus表單組件。https://element-plus.org/zh-CN/component/form.html

  • el-form(綁定表單和規(guī)則對(duì)象)
  • el-form-item(綁定使用的規(guī)則字段)
  • el-input(雙向綁定表單數(shù)據(jù))

當(dāng)功能很復(fù)雜的時(shí)候,通過(guò)多個(gè)組件各自負(fù)責(zé)某個(gè)小功能,再組合成一個(gè)大功能

二、步驟

  • 按照接口字段準(zhǔn)備表單對(duì)象并綁定
  • 按照產(chǎn)品要求準(zhǔn)備規(guī)則對(duì)象并綁定
  • 指定表單域的校驗(yàn)字段名
  • 把表單對(duì)象進(jìn)行雙向綁定

用戶名:不能為空,字段名為account

密碼:不能為空且為6-14個(gè)字符,字段名為password

同意協(xié)議:勾選,字段名為agree

三、前期準(zhǔn)備

在src文件夾下的views文件夾下創(chuàng)建一個(gè)Login文件夾,然后創(chuàng)建一個(gè)Index.vue文件,寫入以下代碼:

<script setup>
import { RouterLink } from 'vue-router';
import { ref } from 'vue';

</script>

<template>
  <div>
    <header class="login-header">
      <div class="container m-top-20">
        <h1 class="logo">
          <RouterLink to="/">小兔鮮</RouterLink>
        </h1>
        <RouterLink class="entry" to="/">
          進(jìn)入網(wǎng)站首頁(yè)
          <i class="iconfont icon-angle-right"></i>
          <i class="iconfont icon-angle-right"></i>
        </RouterLink>
      </div>
    </header>
    <section class="login-section">
      <div class="wrapper">
        <nav>
          <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >賬戶登錄</a>
        </nav>
        <div class="account-box">
          <div class="form">
            <el-form label-position="right" label-width="60px"
              status-icon>
              <el-form-item  label="賬戶">
                <el-input/>
              </el-form-item>
              <el-form-item label="密碼">
                <el-input/>
              </el-form-item>
              <el-form-item label-width="22px">
                <el-checkbox  size="large">
                  我已同意隱私條款和服務(wù)條款
                </el-checkbox>
              </el-form-item>
              <el-button size="large" class="subBtn">點(diǎn)擊登錄</el-button>
            </el-form>
          </div>
        </div>
      </div>
    </section>

    <footer class="login-footer">
      <div class="container">
        <p>
          <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >關(guān)于我們</a>
          <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >幫助中心</a>
          <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >售后服務(wù)</a>
          <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >配送與驗(yàn)收</a>
          <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >商務(wù)合作</a>
          <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >搜索推薦</a>
          <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >友情鏈接</a>
        </p>
        <p>CopyRight &copy; 小兔鮮兒</p>
      </div>
    </footer>
  </div>
</template>

<style scoped lang='scss'>
.login-header {
  background: #fff;
  border-bottom: 1px solid #e4e4e4;

  .container {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }

  .logo {
    width: 200px;

    a {
      display: block;
      height: 132px;
      width: 100%;
      text-indent: -9999px;
      background: url("@/assets/images/logo.png") no-repeat center 18px / contain;
    }
  }

  .sub {
    flex: 1;
    font-size: 24px;
    font-weight: normal;
    margin-bottom: 38px;
    margin-left: 20px;
    color: #666;
  }

  .entry {
    width: 120px;
    margin-bottom: 38px;
    font-size: 16px;

    i {
      font-size: 14px;
      color: $xtxColor;
      letter-spacing: -5px;
    }
  }
}

.login-section {
  background: url('@/assets/images/login-bg.png') no-repeat center / cover;
  height: 488px;
  position: relative;

  .wrapper {
    width: 380px;
    background: #fff;
    position: absolute;
    left: 50%;
    top: 54px;
    transform: translate3d(100px, 0, 0);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);

    nav {
      font-size: 14px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
      text-align: right;
      align-items: center;

      a {
        flex: 1;
        line-height: 1;
        display: inline-block;
        font-size: 18px;
        position: relative;
        text-align: center;
      }
    }
  }
}

.login-footer {
  padding: 30px 0 50px;
  background: #fff;

  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      display: inline-block;

      ~a {
        border-left: 1px solid #ccc;
      }
    }
  }
}

.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;

    a {
      color: $xtxColor;

      i {
        font-size: 14px;
      }
    }
  }

  .form {
    padding: 0 20px 20px 20px;

    &-item {
      margin-bottom: 28px;

      .input {
        position: relative;
        height: 36px;

        >i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }

        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;

          &.error {
            border-color: $priceColor;
          }

          &.active,
          &:focus {
            border-color: $xtxColor;
          }
        }

        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }

      >.error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: $priceColor;

        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }

    .agree {
      a {
        color: #069;
      }
    }

    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: $xtxColor;

      &.disabled {
        background: #cfcdcd;
      }
    }
  }

  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}

.subBtn {
  background: $xtxColor;
  width: 100%;
  color: #fff;
}
</style>

效果圖:

四、實(shí)戰(zhàn)

(1)賬號(hào)和密碼的表單驗(yàn)證

1.按照接口字段準(zhǔn)備表單對(duì)象并綁定

(1)先準(zhǔn)備好表單對(duì)象

// 1.準(zhǔn)備表單對(duì)象
const form =ref({
  account: '',
  password: ''
})

(2)接著做一個(gè)綁定,對(duì)el-form組件進(jìn)行一個(gè)綁定 :model

<el-form :model="form" label-position="right" label-width="60px" status-icon>

2.按照產(chǎn)品要求準(zhǔn)備規(guī)則對(duì)象并綁定

(1)先準(zhǔn)備規(guī)則對(duì)象

// 2.準(zhǔn)備規(guī)則對(duì)象
const rules = {
  account : [
    {required:true , message:'用戶名不能為空!' , trigger :'blur'}
  ],
  password : [
    {required: true ,  message:'密碼不能為空!' , trigger :'blur'},
    {min :6 ,max : 14 , message:'密碼長(zhǎng)度是6-14位!' , trigger :'blur'},
  ]
}
  • required: true表示用戶名(密碼)是必須的,不能為空。
  • message: 是在驗(yàn)證失敗時(shí)顯示的錯(cuò)誤消息。
  • trigger: 'blur'表示當(dāng)用戶離開(kāi)輸入框(失去焦點(diǎn))時(shí)觸發(fā)驗(yàn)證。
  • min:最小長(zhǎng)度
  • max:最大長(zhǎng)度

(2)對(duì)el-form進(jìn)行規(guī)則對(duì)象綁定

<el-form :model="form" :rules="rules" label-position="right" label-width="60px" status-icon>

3.指定表單域的校驗(yàn)字段名

給el-form-item添加校驗(yàn)字段名prop="    "

 <el-form-item prop="account" label="賬戶">
      <el-input/>
 </el-form-item>
 <el-form-item prop="password" label="密碼">
      <el-input/>
 </el-form-item>

4.把表單對(duì)象進(jìn)行雙向綁定

對(duì)el-input進(jìn)行雙向綁定v-model

<el-input v-model="form.account"/>
<el-input v-model="form.password"/>

5.驗(yàn)證看效果

我們可以發(fā)現(xiàn),我們之前設(shè)置的驗(yàn)證規(guī)則都生效的,賬號(hào)密碼都不能為空,它們前面都帶有星號(hào) 

(2)同意協(xié)議的表單驗(yàn)證

使用自定義校驗(yàn)規(guī)則,舉個(gè)栗子

其中三個(gè)參數(shù)分別代表:

  • rule:驗(yàn)證的規(guī)則。
  • value:當(dāng)前輸入的數(shù)據(jù)。
  • callback:一個(gè)回調(diào)函數(shù),用于在驗(yàn)證完成后執(zhí)行。如果驗(yàn)證通過(guò),通常會(huì)調(diào)用callback();如果驗(yàn)證失敗,可能會(huì)調(diào)用callback(new Error('錯(cuò)誤信息'))。

同意協(xié)議的校驗(yàn)邏輯:如果勾選了,通過(guò)校驗(yàn),反之。 

同樣是我們的四個(gè)步驟走:

  • 按照接口字段準(zhǔn)備表單對(duì)象并綁定
// 1.準(zhǔn)備表單對(duì)象
const form =ref({
  account: '',
  password: '',
  agree: true
})

因?yàn)樵谏厦嫖覀円呀?jīng)對(duì)表單對(duì)象進(jìn)行綁定了,這里就不需要了

<el-form :model="form" :rules="rules" label-position="right" label-width="60px" status-icon>?

按照產(chǎn)品要求準(zhǔn)備規(guī)則對(duì)象并綁定

// 2.準(zhǔn)備規(guī)則對(duì)象
const rules = {
  account : [
    {required:true , message:'用戶名不能為空!' , trigger :'blur'}
  ],
  password : [
    {required: true ,  message:'密碼不能為空!' , trigger :'blur'},
    {min :6 ,max : 14 , message:'密碼長(zhǎng)度是6-14位!' , trigger :'blur'},
  ],
  agree : [
    {
      validator:(rule,value,callback) => {
        console.log(value);
        // 勾選就通過(guò),不勾選就不通過(guò)
        if(value){
          callback()
        }else{
          callback(new Error('請(qǐng)勾選協(xié)議!'))
        }
      }
    }
  ]
}

指定表單域的校驗(yàn)字段名

<el-form-item prop="agree" label-width="22px">

把表單對(duì)象進(jìn)行雙向綁定

<el-checkbox v-model="form.agree" size="large">

看效果:

(3)整個(gè)表單內(nèi)容驗(yàn)證

 問(wèn)題:每個(gè)表單域都有自己的檢驗(yàn)觸發(fā)事件,如果用戶一上來(lái)就點(diǎn)擊登錄怎么辦?

 解決:在點(diǎn)擊登錄時(shí)需要對(duì)所有需要檢驗(yàn)的表單進(jìn)行統(tǒng)一檢驗(yàn)。

 步驟:

獲取表單實(shí)例(先獲取一個(gè)form實(shí)例,然后對(duì)其綁定)

// 3.獲取form實(shí)例做同意校驗(yàn)
const formRef = ref(null)
<el-form ref="formRef" :model="form" :rules="rules" label-position="right" label-width="60px" status-icon>

調(diào)用實(shí)例方法(先給登錄按鈕綁定一個(gè)doLogin方法,然后再去定義這個(gè)方法)

 總結(jié)

到此這篇關(guān)于Vue項(xiàng)目表單校驗(yàn)的文章就介紹到這了,更多相關(guān)Vue項(xiàng)目表單校驗(yàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 自定義vue組件發(fā)布到npm的方法

    自定義vue組件發(fā)布到npm的方法

    本篇文章主要介紹了自定義vue組件發(fā)布到npm的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • 詳解Vue2 添加對(duì)scss的支持

    詳解Vue2 添加對(duì)scss的支持

    這篇文章主要介紹了詳解Vue2 添加對(duì)scss的支持,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • element中的el-upload附件上傳與附件回顯

    element中的el-upload附件上傳與附件回顯

    這篇文章主要介紹了element中的el-upload附件上傳與附件回顯方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Vue3響應(yīng)式高階用法之shallowRef()的使用

    Vue3響應(yīng)式高階用法之shallowRef()的使用

    在Vue3框架中,shallowRef()為開(kāi)發(fā)者提供了細(xì)粒度的響應(yīng)式控制能力,特別適用于處理深層嵌套對(duì)象或需要部分響應(yīng)式的場(chǎng)景,本文就來(lái)詳細(xì)的介紹一下,感興趣的可以了解一下
    2024-09-09
  • Vue + ts實(shí)現(xiàn)輪播插件的示例

    Vue + ts實(shí)現(xiàn)輪播插件的示例

    這篇文章主要介紹了Vue + ts實(shí)現(xiàn)輪播插件的示例,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2020-11-11
  • vue3使用element-plus再次封裝table組件的基本步驟

    vue3使用element-plus再次封裝table組件的基本步驟

    這篇文章主要介紹了vue3使用element-plus再次封裝table組件的基本步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-03-03
  • ant?design?vue的form表單取值方法

    ant?design?vue的form表單取值方法

    這篇文章主要介紹了ant?design?vue的form表單取值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Vue.js結(jié)合bootstrap實(shí)現(xiàn)分頁(yè)控件

    Vue.js結(jié)合bootstrap實(shí)現(xiàn)分頁(yè)控件

    這篇文章主要為大家詳細(xì)介紹了Vue.js 合bootstrap實(shí)現(xiàn)分頁(yè)控件的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • vue實(shí)現(xiàn)導(dǎo)航收縮框

    vue實(shí)現(xiàn)導(dǎo)航收縮框

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)導(dǎo)航收縮框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Vite中自制mock服務(wù)器(不使用第三方服務(wù))

    Vite中自制mock服務(wù)器(不使用第三方服務(wù))

    本文主要介紹了Vite中自制mock服務(wù)器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04

最新評(píng)論