天天看點

Element-UI登入頁面案例分享(Demo)

【辰兮要努力】:hello你好我是辰兮,很高興你能來閱讀,昵稱是希望自己能不斷精進,向着優秀程式員前行!

部落格來源于項目以及程式設計中遇到的問題總結,偶爾會有讀書分享,我會陸續更新Java前端、背景、資料庫、項目案例等相關知識點總結,感謝你的閱讀和關注,希望我的部落格能幫助到更多的人,分享擷取新知,大家一起進步!

吾等采石之人,應懷大教堂之心,願你們奔赴在各自的熱愛中…

最近打算系統的整理一下一個vue + element-ui架構的簡單應用。分子產品整理一下demo,以及部分基礎知識,分享給初學者,同時自己也多反思學習。

簡單的分享一個登入vue + element-ui邏輯

Element-UI登入頁面案例分享(Demo)

注意事項:

1、正規表達式的相關校驗(電話号碼,郵箱登入校驗等等)

2、登入 如果成功路由跳轉到下一個頁面 如果失敗彈出提示

相關頁面的詳情,我在如下代碼寫了很詳細的備注,一起學習,共同進步

<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 頭像 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="">
      </div>
      <!-- 表單 -->
      <el-form ref="LoginFormRef" :model="loginForm" label-width="0" :rules="LoginFormRules" class="login_form">
        <el-form-item prop="username">
      <!-- 使用者名-->
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
      <!-- 密碼-->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登入</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        // 登入的初始化資料:備注預設應該是空
        loginForm:{
          username:'',
          password:''
        },
        // 正規表達式的相關校驗
        LoginFormRules:{
          username:[
            { required: true, message: '請輸入使用者名', trigger: 'blur' },
          ],
          password:[
            { required: true, message: '請輸入密碼', trigger: 'blur' },
          ]
        }
      }
    },
    methods: {
      // 清空表單的校驗
      resetLoginForm() {
        this.$refs.LoginFormRef.resetFields()
      },
      //登入的方法:登入邏輯寫裡面
      login() {
        //首先是校驗如果正則的校驗通過 -->> 執行資料傳輸
        this.$refs['LoginFormRef'].validate(async (valid) => {
          if (valid) {
          
            //簡單的在main.js裡面配置了一下如下 
            //Vue.prototype.$http = axios 
            //axios.defaults.baseURL = 'https://localhost:8080/api/private/v1/'

            //執行資料的互動過程 --即對服務端對應接口進行通路
            const {data:res} = await this.$http.post('login',this.loginForm)

            //正常這裡是要分情況寫: 1.如果成功 登入到首頁面 2.如果失敗 彈出對應的提示
            if (res.meta.status==200){
              this.$message({
                message:'登入成功',
                type:'success'
              })
              //如果登入成功存儲token
              window.sessionStorage.setItem('token',res.data.token)
              //跳到首頁面 這裡是根據路由跳轉
              this.$router.push('/home')
            }else{
              this.$message({
                message:res.meta.msg,
                type:'error'
              })
            }
          } else {
            return false
          }
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  .login_container{
    background-color: #2b4b6b;
    height: 100%;
  }

  .login_box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%)
  }

  .avatar_box{
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left:50%;
    transform: translate(-50%,-50%);
    background-color: #fff;
    img{
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }

  .login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 20px;
    box-sizing: border-box;
  }

  .btns{
    display: flex;
    justify-content:flex-end;
  }
</style>>
           

下期Element-UI案例見!

非常感謝你閱讀到這裡,如果這篇文章對你有幫助,希望能留下你的點贊👍 關注❤️ 分享👥 留言💬thanks!!!

2021年2月27日21:54:31 願你們奔赴在自己的熱愛裡!