最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

一、表單檢驗(yàn)

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

2.流程:

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

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

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

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

二、步驟

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

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

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

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

三、前期準(zhǔn)備

在src文件夾下的views文件夾下創(chuàng)建一個Login文件夾,然后創(chuàng)建一個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)站首頁
          <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)賬號和密碼的表單驗(yàn)證

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

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

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

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

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

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

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

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

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

<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.把表單對象進(jìn)行雙向綁定

對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ī)則都生效的,賬號密碼都不能為空,它們前面都帶有星號 

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

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

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

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

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

同樣是我們的四個步驟走:

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

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

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

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

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

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

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

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

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

看效果:

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

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

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

 步驟:

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

// 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í)例方法(先給登錄按鈕綁定一個doLogin方法,然后再去定義這個方法)

 總結(jié)

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

相關(guān)文章

  • vue3?中的toRef函數(shù)和toRefs函數(shù)的基本使用

    vue3?中的toRef函數(shù)和toRefs函數(shù)的基本使用

    這篇文章主要介紹了vue3?toRef函數(shù)和toRefs函數(shù),文中介紹了ref和toRef的區(qū)別,ref本質(zhì)是拷貝,修改響應(yīng)式數(shù)據(jù)不會影響原始數(shù)據(jù),toRef的本質(zhì)是引用關(guān)系,修改響應(yīng)式數(shù)據(jù)會影響原始數(shù)據(jù),需要的朋友可以參考下
    2022-11-11
  • vue 實(shí)現(xiàn)通過vuex 存儲值 在不同界面使用

    vue 實(shí)現(xiàn)通過vuex 存儲值 在不同界面使用

    今天小編就為大家分享一篇vue 實(shí)現(xiàn)通過vuex 存儲值 在不同界面使用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • vue安裝和使用scss及sass與scss的區(qū)別詳解

    vue安裝和使用scss及sass與scss的區(qū)別詳解

    這篇文章主要介紹了vue安裝和使用教程,用了很久css預(yù)編譯器,但是一直不太清楚到底用的sass還是scss,直到有天被問住了有點(diǎn)尷尬,感興趣的朋友一起看看吧
    2018-10-10
  • vue中的適配px2rem示例代碼

    vue中的適配px2rem示例代碼

    這篇文章主要給大家介紹了關(guān)于vue中適配px2rem的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Vue插槽檢測之$slots的妙用與最佳實(shí)踐指南

    Vue插槽檢測之$slots的妙用與最佳實(shí)踐指南

    這篇文章主要介紹了Vue插槽檢測之$slots的妙用與最佳實(shí)踐指南的相關(guān)資料,$slots是一個具有只讀屬性的對象,用來訪問被插槽分發(fā)的內(nèi)容,文中將用法介紹的非常詳細(xì),需要的朋友可以參考下
    2026-03-03
  • Vue3中createWebHistory和createWebHashHistory的區(qū)別詳析

    Vue3中createWebHistory和createWebHashHistory的區(qū)別詳析

    這篇文章主要給大家介紹了關(guān)于Vue3中createWebHistory和createWebHashHistory區(qū)別的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-06-06
  • Vue3中Vuex狀態(tài)管理學(xué)習(xí)實(shí)戰(zhàn)示例詳解

    Vue3中Vuex狀態(tài)管理學(xué)習(xí)實(shí)戰(zhàn)示例詳解

    這篇文章主要為大家介紹了Vue3中Vuex狀態(tài)管理學(xué)習(xí)實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • vue-router 權(quán)限控制的示例代碼

    vue-router 權(quán)限控制的示例代碼

    本篇文章主要介紹了vue-router 權(quán)限控制的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Vue3實(shí)現(xiàn)自定義指令攔截點(diǎn)擊事件的示例代碼

    Vue3實(shí)現(xiàn)自定義指令攔截點(diǎn)擊事件的示例代碼

    某些應(yīng)用場景會給點(diǎn)擊事件添加權(quán)限,不存在權(quán)限就攔截點(diǎn)擊事件,有權(quán)限就繼續(xù)正常觸發(fā)點(diǎn)擊事件。這樣的效果是如何實(shí)現(xiàn)的呢,本文就來和大家詳細(xì)講講
    2023-02-02
  • vue中使用$emit傳遞多個參數(shù)的2種方法

    vue中使用$emit傳遞多個參數(shù)的2種方法

    這篇文章主要給大家介紹了關(guān)于vue中如何使用$emit傳遞多個參數(shù)的2種方法,在Vue中可以使用$emit方法向父組件傳遞數(shù)據(jù),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10

最新評論

旬邑县| 宣恩县| 廊坊市| 邹平县| 大港区| 闸北区| 平原县| 延吉市| 无极县| 遵义市| 丹巴县| 轮台县| 松溪县| 南乐县| 丽水市| 镇康县| 永寿县| 黄浦区| 南漳县| 肇州县| 沾益县| 辽源市| 平果县| 洛南县| 隆昌县| 喀什市| 新建县| 万载县| 方山县| 明溪县| 枣阳市| 保亭| 且末县| 肇州县| 鸡泽县| 松滋市| 安阳县| 石泉县| 芮城县| 玉屏| 天水市|