我的第一個項目(四):(前端)發送請求以及表單校驗

来源:https://www.cnblogs.com/FatTiger4399/archive/2022/11/30/16939586.html
-Advertisement-
Play Games

好家伙,本篇將繼續完善前端界面 效果展示: 1.註冊登陸 (後端已啟動) 2.註冊表單驗證 (前端實現的表單驗證) 在此之前: 我的第一個項目(二):使用Vue做一個登錄註冊界面 - 養肥胖虎 - 博客園 (cnblogs.com) 後端部分: 我的第一個項目(三):註冊登陸功能(後端) - 養肥胖 ...


好家伙,本篇將繼續完善前端界面

 

效果展示:

1.註冊登陸

(後端已啟動)

 

 

 

 

2.註冊表單驗證

(前端實現的表單驗證)

 

 

 

 

在此之前:

我的第一個項目(二):使用Vue做一個登錄註冊界面 - 養肥胖虎 - 博客園 (cnblogs.com)

後端部分:

我的第一個項目(三):註冊登陸功能(後端) - 養肥胖虎 - 博客園 (cnblogs.com)

 

1.前端發送網路請求(axios)

項目目錄:

 

Mylogin.vue文件:

<template>
  <div class="login-container">
    <div class="login-box">

      <!-- 頭像區域 -->
      <div class="text-center avatar-box">
        <img src="../assets/logo.png" class="img-thumbnail avatar" alt="">
      </div>

      <!-- 表單區域 -->
      <div class="form-login p-4">
        <!-- 登錄名稱 -->
        <div class="form-group form-inline">
          <label for="username">賬號:</label>
          <input type="text" class="form-control ml-2" id="username" placeholder="請輸入賬號" autocomplete="off" v-model.trim="loginForm.loginName"/>
        </div>
        <!-- 登錄密碼 -->
        <div class="form-group form-inline">
          <label for="password">密碼:</label>
          <input type="password" class="form-control ml-2" id="password" placeholder="請輸入密碼" v-model.trim="loginForm.password"/>
        </div>
        <!-- 登錄和重置按鈕 -->
        <div class="form-group form-inline d-flex justify-content-end">
          <button type="button" class="btn btn-secondary mr-2" @click="toregister">去註冊</button>
          <button type="button" class="btn btn-primary" @click="login">登錄</button>
        </div>
      </div>

    </div>
  </div>
</template>

<script>
export default {
  name: 'MyLogin',
  data() {
    return {
      loginForm: {
          loginName: '',
          password: ''
        }
    }
  },
  methods: {
    
    login() {
        // console.log('submit!',this.loginForm);
        
            this.axios.post('http://localhost:3312/sys-user/login',this.loginForm).then((resp)=>{
              console.log(resp);
                let data = resp.data;
                if(data.success){
                    this.loginForm= {};
                    this.$message({
                    message: '登陸成功!!!',
                    type: 'success'
                    });
                    this.$router.push({path:'/Home'})
                } else {

                  console.log(data)

                }
            })

      },
    toregister(){
      this.$router.push('/register')
    },
  }
}
</script>

<style lang="less" scoped>
.login-container {
  background-color: #35495e;
  height: 100%;
  .login-box {
    width: 400px;
    height: 250px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 6px rgba(255, 255, 255, 0.5);
    .form-login {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      box-sizing: border-box;
    }
  }
}

.form-control {
  flex: 1;
}

.avatar-box {
  position: absolute;
  width: 100%;
  top: -65px;
  left: 0;
  .avatar {
    width: 120px;
    height: 120px;
    border-radius: 50% !important;
    box-shadow: 0 0 6px #efefef;
  }
}
</style>

 

MyRegister.vue文件:

<template>
    <div class="login-container">
        <div class="login-box">

            <!-- 頭像區域 -->
            <div class="text-center avatar-box">
                <img src="../assets/logo.png" class="img-thumbnail avatar" alt="">
            </div>

            <!-- 表單區域 -->
            <div class="form-login p-4">
                <!-- 登錄名稱 -->
                <!--.trim用於去除首位空格  -->
                <div class="form-group form-inline">
                    <label for="username">賬戶:</label>
                    <input type="text" class="form-control ml-2" id="username" placeholder="請輸入昵稱" autocomplete="off"
                        v-model.trim="ruleForm.loginName" />
                </div>

                <!-- 登錄昵稱 -->
                <div class="form-group form-inline">
                    <label for="password">昵稱:</label>
                    <input type="name" class="form-control ml-2" id="name" placeholder="請輸入手機號"
                        v-model.trim="ruleForm.name" />
                </div>
                <!-- 登錄密碼 -->
                <div class="form-group form-inline">
                    <label for="password">密碼:</label>
                    <input type="password" class="form-control ml-2" id="password" placeholder="請輸入登錄密碼"
                        v-model.trim="ruleForm.password" />
                </div>
                <!-- 確認密碼 -->
                <div class="form-group form-inline">
                    <label for="password">確認密碼:</label>
                    <input type="trypassword" class="form-control ml-2" id="password" placeholder="請再次輸入登錄密碼"
                        v-model.trim="ruleForm.checkPass" />
                </div>
                <!-- 登錄和重置按鈕 -->
                <div class="form-group form-inline d-flex justify-content-end">
                    <button type="button" class="btn btn-secondary mr-2" @click="backlogin">返回登陸界面</button>
                    <button type="button" class="btn btn-primary" @click="submitForm(ruleForm)">註冊</button>
                </div>
            </div>

        </div>
    </div>
</template>
  
<script>
export default {

    name: 'MyRegister',
    data() {
        return {
            ruleForm: {
                loginName: '',
                password: '',
                name: '',
                checkPass: ""
            },
        }
    },
    methods: {
        backlogin() {
            this.$router.push('/login')
            return
        },
        submitForm(ruleForm) {
            //校驗部分
            if (this.ruleForm.password != this.ruleForm.checkPass) {
                this.$message({
                    message: '兩次輸入密碼不一致!!!',
                    type: 'error'
                });
            }
            else if(this.ruleForm.loginName==""){
                this.$message({
                    message: '請輸入賬戶!!!',
                    type: 'error'
                });
            }
            else if(this.ruleForm.name==""){
                this.$message({
                    message: '請輸入昵稱!!!',
                    type: 'error'
                });
            }
            else if(this.ruleForm.password==""){
                this.$message({
                    message: '請輸入密碼!!!',
                    type: 'error'
                });
            }
            else {
                this.axios.post('http://localhost:3312/sys-user/register', this.ruleForm).then((resp) => {
                    console.log(resp);
                    
                    let data = resp.data;
                    console.log(data);
                    if (data.success) {
                        this.ruleForm = {};
                        this.$message({
                            message: '恭喜你,註冊成功,點擊去登陸按鈕進行登陸吧!!!',
                            type: 'success'
                        });
                    }
                })
                this.$router.push({ path: '/login' })
            }

        },
    }
}
</script>
  
<style lang="less" scoped>
.login-container {
    background-color: #35495e;
    height: 100%;

    .login-box {
        width: 400px;
        height: 320px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        box-shadow: 0 0 6px rgba(255, 255, 255, 0.5);

        .form-login {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            box-sizing: border-box;
        }
    }
}

.form-control {
    flex: 1;
}

.avatar-box {
    position: absolute;
    width: 100%;
    top: -65px;
    left: 0;

    .avatar {
        width: 120px;
        height: 120px;
        border-radius: 50% !important;
        box-shadow: 0 0 6px #efefef;
    }
}
</style>

 

請求部分:

{
                //發起網路請求,
                this.axios.post('http://localhost:3312/sys-user/register', this.ruleForm).then((resp) => {
                    console.log(resp);
                    
                    let data = resp.data;
                    console.log(data);
                    //檢驗註冊是否成功
                    if (data.success) {
                        //清空表單數據
                        this.ruleForm = {};
                        //發送成功通知
                        this.$message({
                            message: '恭喜你,註冊成功,點擊去登陸按鈕進行登陸吧!!!',
                            type: 'success'
                        });
                    }
                })
                this.$router.push({ path: '/login' })
            }

 

2.表單驗證:

if (this.ruleForm.password != this.ruleForm.checkPass) {
                this.$message({
                    message: '兩次輸入密碼不一致!!!',
                    type: 'error'
                });
            }
            else if(this.ruleForm.loginName==""){
                this.$message({
                    message: '請輸入賬戶!!!',
                    type: 'error'
                });
            }
            else if(this.ruleForm.name==""){
                this.$message({
                    message: '請輸入昵稱!!!',
                    type: 'error'
                });
            }
            else if(this.ruleForm.password==""){
                this.$message({
                    message: '請輸入密碼!!!',
                    type: 'error'
                });
            }

這個沒什麼好解釋的,就是很簡單的驗證

如果使用的是組件的表單(比如:element之類的)也可以編輯相應的表單驗證方法(規則)

當然,自己手寫也沒什麼問題

 

 

補充:資料庫建表

(前面好像一直都忘了)

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 用acme.sh自動部署功能變數名稱證書 安裝ACME 目前使用量最大的免費SSL證書就是Let’s Encrypt,自2018-03開始,Let’s Encrypt官方發佈上線了免費的SSL泛功能變數名稱證書,目前通過DNS方式獲取比較快,國內可以通過鵝雲的DNSPod功能變數名稱API或者貓雲功能變數名稱API自動簽發Let’ ...
  • public static void GetRegistData() { string name = "huishuangzhu"; //搜索到註冊表根目錄 RegistryKey hkml = Registry.ClassesRoot; //搜索到註冊表根目錄下的XXX文件夾。 RegistryK ...
  • Linux下用rm誤刪除文件的三種恢復方法 對於rm,很多人都有慘痛的教訓。我也遇到一次,一下午寫的程式就被rm掉了,幸好只是一個文件,第二天很快又重新寫了一遍。但是很多人可能就不像我這麼幸運了。本文收集了一些在Linux下恢復rm刪除的文件的方法,給大家作為參考。 1.幾點建議避免誤刪 首先,最好 ...
  • 大數據時代,資料庫 SaaS 是企業實現降本增效和業務創新的重要抓手。在騰訊全球數字生態大會資料庫 SaaS 專場上,騰訊雲發佈了多項資料庫 SaaS 產品能力升級,並重點分享了其在上雲、日常運維、資料庫遷移等多方面的實踐應用,為廣大企業構建和提升自身數據能力提供了有效參考。 騰訊雲資料庫副總經理羅 ...
  • 大量的數據科學職位需要精通 SQL,它也是數據分析師、數據科學家、數據建模崗最常考核的面試技能。在本篇內容中 ShowMeAI 將梳理彙總所有面試 SQL 問題,按照不同的主題構建練習專項塊。 ...
  • 閱識風雲是華為雲信息大咖,擅長將複雜信息多元化呈現,其出品的一張圖(雲圖說)、深入淺出的博文(雲小課)或短視頻(雲視廳)總有一款能讓您快速上手華為雲。更多精彩內容請單擊此處。 摘要:購買Redis實例時,實例類型有單機、主備、Proxy集群、Cluster集群和讀寫分離這麼多種,該怎麼選?別擔心,本 ...
  • TIS整合ChunJun實操 B站視頻: https://www.bilibili.com/video/BV1QM411z7w5/?spm_id_from=333.999.0.0 一、ChunJun 概述 ChunJun是一款易用、穩定、高效的批流統一的數據集成框架,可基於實時計算引擎Flink實現 ...
  • 當我們把介面都做好以後,我們需要去開發前端界面。 添加文章功能裡面,最重要的就是文章內容部分,需要配置上富文本編輯器,這樣才能給我們的內容增加樣式。 下載ueditor代碼 ueditor已經很久沒有更新了,我們現在去github下載壓縮好的代碼包 https://github.com/fex-te ...
一周排行
    -Advertisement-
    Play Games
  • 1. 說明 /* Performs operations on System.String instances that contain file or directory path information. These operations are performed in a cross-pla ...
  • 視頻地址:【WebApi+Vue3從0到1搭建《許可權管理系統》系列視頻:搭建JWT系統鑒權-嗶哩嗶哩】 https://b23.tv/R6cOcDO qq群:801913255 一、在appsettings.json中設置鑒權屬性 /*jwt鑒權*/ "JwtSetting": { "Issuer" ...
  • 引言 集成測試可在包含應用支持基礎結構(如資料庫、文件系統和網路)的級別上確保應用組件功能正常。 ASP.NET Core 通過將單元測試框架與測試 Web 主機和記憶體中測試伺服器結合使用來支持集成測試。 簡介 集成測試與單元測試相比,能夠在更廣泛的級別上評估應用的組件,確認多個組件一起工作以生成預 ...
  • 在.NET Emit編程中,我們探討了運算操作指令的重要性和應用。這些指令包括各種數學運算、位操作和比較操作,能夠在動態生成的代碼中實現對數據的處理和操作。通過這些指令,開發人員可以靈活地進行算術運算、邏輯運算和比較操作,從而實現各種複雜的演算法和邏輯......本篇之後,將進入第七部分:實戰項目 ...
  • 前言 多表頭表格是一個常見的業務需求,然而WPF中卻沒有預設實現這個功能,得益於WPF強大的控制項模板設計,我們可以通過修改控制項模板的方式自己實現它。 一、需求分析 下圖為一個典型的統計表格,統計1-12月的數據。 此時我們有一個需求,需要將月份按季度劃分,以便能夠直觀地看到季度統計數據,以下為該需求 ...
  • 如何將 ASP.NET Core MVC 項目的視圖分離到另一個項目 在當下這個年代 SPA 已是主流,人們早已忘記了 MVC 以及 Razor 的故事。但是在某些場景下 SSR 還是有意想不到效果。比如某些靜態頁面,比如追求首屏載入速度的時候。最近在項目中回歸傳統效果還是不錯。 有的時候我們希望將 ...
  • System.AggregateException: 發生一個或多個錯誤。 > Microsoft.WebTools.Shared.Exceptions.WebToolsException: 生成失敗。檢查輸出視窗瞭解更多詳細信息。 內部異常堆棧跟蹤的結尾 > (內部異常 #0) Microsoft ...
  • 引言 在上一章節我們實戰了在Asp.Net Core中的項目實戰,這一章節講解一下如何測試Asp.Net Core的中間件。 TestServer 還記得我們在集成測試中提供的TestServer嗎? TestServer 是由 Microsoft.AspNetCore.TestHost 包提供的。 ...
  • 在發現結果為真的WHEN子句時,CASE表達式的真假值判斷會終止,剩餘的WHEN子句會被忽略: CASE WHEN col_1 IN ('a', 'b') THEN '第一' WHEN col_1 IN ('a') THEN '第二' ELSE '其他' END 註意: 統一各分支返回的數據類型. ...
  • 在C#編程世界中,語法的精妙之處往往體現在那些看似微小卻極具影響力的符號與結構之中。其中,“_ =” 這一組合突然出現還真不知道什麼意思。本文將深入剖析“_ =” 的含義、工作原理及其在實際編程中的廣泛應用,揭示其作為C#語法奇兵的重要角色。 一、下劃線 _:神秘的棄元符號 下劃線 _ 在C#中並非 ...