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

来源: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
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...