使用vue制作简单的登录页面(含验证码)

话不多说,先看结果,时间匆忙,样式没有怎么调整,能看就行,图片都懒得换了 qwq…
同样,为了进行后续的拓展,使用的简单的路由功能(其实就是懒得换)
在这里插入图片描述

首先便是项目的创建,这个不再过多叙述,接下来修改部分文件:
1.index.html中添加样式,修改背景颜色在这里插入图片描述

<style>
      body{
        background: #fff1ff;
      }
    </style>

2.src文件夹下新建login登录组件 login.vue

<template>
    <div id="border_div">
      <div class="show_div">
        <div class="name_div">
          <span>姓名:</span>
          <input type="text" v-model="username" @blur="checkName" @keyup.enter="$event.target.blur" placeholder="请输入用户名称">
        </div>
      </div>
      <div class="show_div">
        <div class="password_div">
          <span>密码:</span>
          <input type="password" v-model="userword" @blur="checkWord" @keyup.enter="$event.target.blur" placeholder="请输入用户密码">
        </div>
      </div>
      <div class="showYZM_div">
        <div class="yzm_div">
          <span>验证码:</span>
          <input type="text" v-model="check_yzm" @blur="checkYZM" @keyup.enter="$event.target.blur" placeholder="请输入验证码">
        </div>
        <div class="yzm_picture_div">
          <span v-model="yzm_code">{{yzm_code}}</span>
        </div>
        <div class="yzm_text_div">
          <a href="javascript:void(0);" @click="changeYZM">看不清,换一张试试?</a>
        </div>
      </div>
      <div>
        <div>
          <button @click="login">登录</button>
          <button @click="reset">取消</button>
          <a to="/">忘记密码</a>
        </div>
        <div style="color: red">
          <p v-show="showErrorMssage">{{ErrorMessage}}</p>
        </div>
      </div>

    </div>
</template>

<script>
    export default {
        name: "login",
        data(){
          return{
            username:'',
            userword:'',
            yzm_code:'axaz',
            check_yzm:'',
            check_yzm_status:false,
            yzm_code_length:4,
            ErrorMessage:'',
            showErrorMssage:false,
            user:{name:'niuningfei',password:'123456'}
          }
        },
      computed:{
      init(){
        console.log("初始化验证码---axaz")
      }
      },
      methods:{
        changeYZM:function(){
          let yzm_code = '';//点击更换时-初始验证码
          let yzm_code_length = this.yzm_code_length;//验证码长度获取
          let codeChars = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');//候选字符
          //通过循环获取yzm_code_length位codeChars中的字符下标,并通过下标获取指定字符,最后拼接
          for (let i = 0; i < yzm_code_length; i++ ) {
            let charNum = Math.floor(Math.random() * 62);//获取随机验证码下标
            yzm_code += codeChars[charNum];//根据下标获取指定字符并拼接
          }
          console.log("修改后验证码---"+yzm_code);
          return this.yzm_code = yzm_code;
        },
        checkName:function () {
          if(this.username == ''){
            this.ErrorMessage = '用户名不能为空';
            this.showErrorMssage = true;
            return;
          }else if(this.username != this.user.name){
            this.ErrorMessage = '用户名输入有误';
            this.showErrorMssage = true;
            return;
          }else {
            this.ErrorMessage = '';
            this.showErrorMssage = false;
          }
        },
        checkWord:function () {
          if(this.userword == ''){
            this.ErrorMessage = '用户密码不能为空';
            this.showErrorMssage = true;
            return;
          }else if(this.userword != this.user.password){
            this.ErrorMessage = '用户密码输入有误';
            this.showErrorMssage = true;
            return;
          }else {
            this.ErrorMessage = '';
            this.showErrorMssage = false;
          }
        },
        checkYZM:function () {
          if(this.check_yzm == ''){
            this.ErrorMessage = '验证码不能为空';
            this.showErrorMssage = true;
            return;
          }else if(this.check_yzm.toUpperCase() != this.yzm_code.toUpperCase()){
            console.log("待校验验证码---"+this.check_yzm.toUpperCase())
            console.log("正确验证码---"+this.yzm_code.toUpperCase())
            this.ErrorMessage = '验证码输入有误';
            this.showErrorMssage = true;
            return;
          }else {
            this.ErrorMessage = '';
            this.showErrorMssage = false;
            this.check_yzm_status = true;
          }
        },
        login:function () {
          if(this.username == this.user.name && this.userword == this.user.password){
            if(this.check_yzm_status){
              console.log("用户登录---"+this.username+"---"+this.userword);
              this.ErrorMessage = '登录成功,欢迎您';
              this.showErrorMssage = true;
            }else {
              this.ErrorMessage = '请再次校验验证码';
              this.showErrorMssage = true;
              return;
            }
          }
        },
        reset:function () {
          this.username = '';
          this.userword = '';
          this.yzm_code = '';
          this.check_yzm = '';
          this.check_yzm_status = false;
          this.ErrorMessage = '请重新输入用户信息';
          this.showErrorMssage = true;
          this.check_yzm_status = false;
        }

      }
    }
</script>

<style scoped>
  #border_div{
    width: 500px;
    height: 300px;
    background: #ffdad6;
    text-align: center;
    display: inline-block;
    vertical-align: middle;
    position: absolute;
    border-radius: 15px;
    margin: auto;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }
  .show_div{
    width: 500px;
    height: 30px;
    position: relative;
  }
  .showYZM_div{
    width: 500px;
    height: 80px;
    position: relative;
  }
  .yzm_picture_div{
    width: 240px;
    margin-left: 130px;
    margin-top: 5px;
    margin-bottom: 5px;
    background: floralwhite;
    letter-spacing: 35px;
    color: orangered;
  }
  .yzm_picture_div{
    font-size: 15px;
  }
  .name_div{
    margin-top: 30px;
  }
  .password_div{
    margin-top: 30px;
  }
  .yzm_div{
    margin-top: 30px;
  }
</style>

接下来详细讲解下整个实现代码:
模版显示区:包括用户名称区域,用户密码区域,验证码区域,登录/取消区域,错误/成功信息提示区域(默认隐藏)

<template>
    <div id="border_div">
      <div class="show_div">
        <div class="name_div">
          <span>姓名:</span>
          <input type="text" v-model="username" @blur="checkName" @keyup.enter="$event.target.blur" placeholder="请输入用户名称">
        </div>
      </div>
      <div class="show_div">
        <div class="password_div">
          <span>密码:</span>
          <input type="password" v-model="userword" @blur="checkWord" @keyup.enter="$event.target.blur" placeholder="请输入用户密码">
        </div>
      </div>
      <div class="showYZM_div">
        <div class="yzm_div">
          <span>验证码:</span>
          <input type="text" v-model="check_yzm" @blur="checkYZM" @keyup.enter="$event.target.blur" placeholder="请输入验证码">
        </div>
        <div class="yzm_picture_div">
          <span v-model="yzm_code">{{yzm_code}}</span>
        </div>
        <div class="yzm_text_div">
          <a href="javascript:void(0);" @click="changeYZM">看不清,换一张试试?</a>
        </div>
      </div>
      <div>
        <div>
          <button @click="login">登录</button>
          <button @click="reset">取消</button>
          <a to="/">忘记密码</a>
        </div>
        <div style="color: red">
          <p v-show="showErrorMssage">{{ErrorMessage}}</p>
        </div>
      </div>

    </div>
</template>

script区块:
里面包括:
1.信息初始化和computed方法(log显示初始化验证码信息,意义不大)

name: "login",
        data(){
          return{
            username:'',
            userword:'',
            yzm_code:'axaz',
            check_yzm:'',
            check_yzm_status:false,
            yzm_code_length:4,
            ErrorMessage:'',
            showErrorMssage:false,
            user:{name:'niuningfei',password:'123456'}
          }
        },
      computed:{
      init(){
        console.log("初始化验证码---axaz")
      }
      },

2.方法区块中:
更换验证码

changeYZM:function(){
          let yzm_code = '';//点击更换时-初始验证码
          let yzm_code_length = this.yzm_code_length;//验证码长度获取
          let codeChars = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');//候选字符
          //通过循环获取yzm_code_length位codeChars中的字符下标,并通过下标获取指定字符,最后拼接
          for (let i = 0; i < yzm_code_length; i++ ) {
            let charNum = Math.floor(Math.random() * 62);//获取随机验证码下标
            yzm_code += codeChars[charNum];//根据下标获取指定字符并拼接
          }
          console.log("修改后验证码---"+yzm_code);
          return this.yzm_code = yzm_code;
        }

用户名、用户密码、验证码规则校验

checkName:function () {
          if(this.username == ''){
            this.ErrorMessage = '用户名不能为空';
            this.showErrorMssage = true;
            return;
          }else if(this.username != this.user.name){
            this.ErrorMessage = '用户名输入有误';
            this.showErrorMssage = true;
            return;
          }else {
            this.ErrorMessage = '';
            this.showErrorMssage = false;
          }
        },
        checkWord:function () {
          if(this.userword == ''){
            this.ErrorMessage = '用户密码不能为空';
            this.showErrorMssage = true;
            return;
          }else if(this.userword != this.user.password){
            this.ErrorMessage = '用户密码输入有误';
            this.showErrorMssage = true;
            return;
          }else {
            this.ErrorMessage = '';
            this.showErrorMssage = false;
          }
        },
        checkYZM:function () {
          if(this.check_yzm == ''){
            this.ErrorMessage = '验证码不能为空';
            this.showErrorMssage = true;
            return;
          }else if(this.check_yzm.toUpperCase() != this.yzm_code.toUpperCase()){
            console.log("待校验验证码---"+this.check_yzm.toUpperCase())
            console.log("正确验证码---"+this.yzm_code.toUpperCase())
            this.ErrorMessage = '验证码输入有误';
            this.showErrorMssage = true;
            return;
          }else {
            this.ErrorMessage = '';
            this.showErrorMssage = false;
            this.check_yzm_status = true;
          }
        }

3.登录、取消登陆方法

login:function () {
          if(this.username == this.user.name && this.userword == this.user.password){
            if(this.check_yzm_status){
              console.log("用户登录---"+this.username+"---"+this.userword);
              this.ErrorMessage = '登录成功,欢迎您';
              this.showErrorMssage = true;
            }else {
              this.ErrorMessage = '请再次校验验证码';
              this.showErrorMssage = true;
              return;
            }
          }
        },
        reset:function () {
          this.username = '';
          this.userword = '';
          this.yzm_code = '';
          this.check_yzm = '';
          this.check_yzm_status = false;
          this.ErrorMessage = '请重新输入用户信息';
          this.showErrorMssage = true;
          this.check_yzm_status = false;
        }

      }

接下来修改src/router/index.js文件:

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Login from '../login.vue'
Vue.use(Router)

export default new Router({
  mode:'history',
  base:__dirname,
  routes: [
    {
      path: '/',
      component: Login
    },
    {
      path: '/Login',
      component: Login
    },
    {
      path: '/HelloWorld',
      component: HelloWorld
    }
  ]
})

最后便是src/APP.vue文件中添加三个路由链接
在这里插入图片描述

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-link to="/">/Home</router-link>
    <router-link to="/Login">/login</router-link>
    <router-link to="/HelloWorld">/HelloWorld</router-link>
    <router-view/>
  </div>
</template>

<script>

export default {
  name: 'App'
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

大功告成!!!!

  • 11
    点赞
  • 78
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,下面是一个简单的登录注册界面的示例代码: ``` <template> <div class="container"> <div class="login-form"> <h2>Login</h2> <form @submit.prevent="login"> <div class="form-group"> <label for="email">Email address</label> <input type="email" class="form-control" id="email" v-model="loginForm.email" required> </div> <div class="form-group"> <label for="password">Password</label> <input type="password" class="form-control" id="password" v-model="loginForm.password" required> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> <div class="register-form"> <h2>Register</h2> <form @submit.prevent="register"> <div class="form-group"> <label for="name">Name</label> <input type="text" class="form-control" id="name" v-model="registerForm.name" required> </div> <div class="form-group"> <label for="email">Email address</label> <input type="email" class="form-control" id="email" v-model="registerForm.email" required> </div> <div class="form-group"> <label for="password">Password</label> <input type="password" class="form-control" id="password" v-model="registerForm.password" required> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> </div> </template> <script> export default { data() { return { loginForm: { email: '', password: '' }, registerForm: { name: '', email: '', password: '' } }; }, methods: { login() { // 发送登录请求 }, register() { // 发送注册请求 } } }; </script> <style> .container { display: flex; justify-content: center; align-items: center; height: 100vh; } .login-form, .register-form { width: 400px; padding: 20px; border: 1px solid #ccc; border-radius: 5px; } .login-form h2, .register-form h2 { text-align: center; margin-bottom: 20px; } </style> ``` 这个示例代码包了一个登录表单和一个注册表单,使用了Bootstrap样式。你可以根据自己的需要进行修改和扩展。需要注意的是,这个示例代码只包了前端部分,实现登录注册功能需要与后端进行交互。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值