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

来源: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
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...