【Vue2注册登录界面】Vue2+elementUI编写一个登录页面,路由式开发,后台管理系统登录界面

该文章展示了如何使用Vue.js创建一个登录页面,包括模板部分(HTML结构),样式部分(使用Less编写),以及Vue组件的实现。表单验证通过自定义rules完成,提交按钮触发axios接口调用以执行登录操作。页面设计简洁,代码结构清晰。
摘要由CSDN通过智能技术生成

目录

效果图

1.template部分

2.style部分

3.vue部分

(1).引入封装的axios接口,方便后面联调

(2)表单

(3).methods部分

4.完整代码



效果图

1.template部分

<div>
    <el-form ref="form" label-width="70px" :inline="true" class="login-container" :model="form" :rules="rules">
      <h3 class="login_title">系统登录</h3>

        <el-form-item label="用户名" prop="userName">
            <el-input v-model="form.userName" placeholder="请输入账号"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
            <el-input show-password v-model="form.password" placeholder="请输入密码"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button @click="submit" class="login_button" type="primary"> 登 录 </el-button>
        </el-form-item>


    </el-form>
  </div>

2.style部分

这里我用的是less编写,

需要下载less和less-loader

<style lang="less" scoped>
.login-container {
  width: 400px;
  border:1px solid #eaeaea;
  margin: 180px auto;
  padding: 35px 35px 15px 35px;
  border-radius: 15px;
  box-shadow: 0 0 25px #cac6c6;
  background-color: #ffffff;
  box-sizing: border-box;
  .login_title {
    text-align: center;
    margin-bottom: 40px;
    color: #505458;
  }
  .el-input {
    width: 198px;
  }
  .login_button {
    margin-left: 105px;
    margin-top: 10px;
  }
  .login_register {
    width: 70px;
    height: 40px;
    text-align: center;
    margin-top: 10px;
  }
}

3.vue部分

(1).引入封装的axios接口,方便后面联调

import {GetLogin} from "@/Api/api";

这里每个人的都不一样,这只是我的封装

(2)表单

trigger触发方式Stringclick/focus/hover/manualclick
required是否必填,如不设置,则会根据校验规则自动生成boolean

false

blur在 Input 失去焦点时触发(event: Event)
data() {
    return {
      form: {
        userName:'',
        password:'',
      },
      rules: {
        userName: [
          {
            required: true,
            trigger: 'blur',
            message: '长度在6到16个字符',
            min: 6, max: 16,
          }
        ],
        password: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入密码',
            min: 6, max: 16,

          }
        ],
      }
    }
  },

(3).methods部分

validate对整个表单进行校验的方法,参数为一个回调函数。该回调函数会在校验结束后被调用,并传入两个参数:是否校验成功和未通过校验的字段。若不传入回调函数,则会返回一个 promiseFunction(callback: Function(boolean, object))
methods: {
    //提交方法
    submit() {
        this.$refs.validate((valid) => {
          if (valid) {
            // 封装的接口使用
            GetLogin({
              // 表单的数据
              userName: this.form.userName,
              password: this.form.password,
            }).then(({data}) => {
              // console.log(data)
              if (data.code === 200) {
                //将token存储在本地
                localStorage.setItem('token',data.data.token)
                //若登录成功则跳转到指定路由
                this.$router.push('/')
              } else {
                this.$message.error(data.data.rules.message)
              }
            })
          }
        })
    }
  }

4.完整代码

<template>
  <div>
    <el-form ref="form" label-width="70px" :inline="true" class="login-container" :model="form" :rules="rules">
      <h3 class="login_title">系统登录</h3>

        <el-form-item label="用户名" prop="userName">
            <el-input v-model="form.userName" placeholder="请输入账号"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
            <el-input show-password v-model="form.password" placeholder="请输入密码"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button @click="submit" class="login_button" type="primary"> 登 录 </el-button>
        </el-form-item>


    </el-form>
  </div>
</template>

<script>
import {GetLogin} from "@/Api/api";

export default {
  name: "AppLogin",
  data() {
    return {
      form: {
        userName:'',
        password:'',
      },
      rules: {
        userName: [
          {
            required: true,
            trigger: 'blur',
            message: '长度在6到16个字符',
            min: 6, max: 16,
          }
        ],
        password: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入密码',
            min: 6, max: 16,

          }
        ],
      }
    }
  },
  methods: {
    //提交方法
    submit() {
        this.$refs.validate((valid) => {
          if (valid) {
            // 封装的接口使用
            GetLogin({
              // 表单的数据
              userName: this.form.userName,
              password: this.form.password,
            }).then(({data}) => {
              // console.log(data)
              if (data.code === 200) {
                //将token存储在本地
                localStorage.setItem('token',data.data.token)
                //若登录成功则跳转到指定路由
                this.$router.push('/')
              } else {
                this.$message.error(data.data.rules.message)
              }
            })
          }
        })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  width: 400px;
  border:1px solid #eaeaea;
  margin: 180px auto;
  padding: 35px 35px 15px 35px;
  border-radius: 15px;
  box-shadow: 0 0 25px #cac6c6;
  background-color: #ffffff;
  box-sizing: border-box;
  .login_title {
    text-align: center;
    margin-bottom: 40px;
    color: #505458;
  }
  .el-input {
    width: 198px;
  }
  .login_button {
    margin-left: 105px;
    margin-top: 10px;
  }
  .login_register {
    width: 70px;
    height: 40px;
    text-align: center;
    margin-top: 10px;
  }
}
</style>

若有不足或错误之处,欢迎指点

  • 6
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
以下是一个Vue2登录注册页面的代码示例: ```html <template> <div> <el-form ref="form" label-width="70px" :inline="true" class="login-container" :model="form" :rules="rules"> <h3 class="login_title">系统登录</h3> <el-form-item label="用户名" prop="userName"> <el-input v-model="form.userName" placeholder="请输入账号"></el-input> </el-form-item> <el-form-item label="密码" prop="password"> <el-input show-password v-model="form.password" placeholder="请输入密码"></el-input> </el-form-item> <el-form-item> <el-button @click="submit" class="login_button" type="primary">登录</el-button> </el-form-item> </el-form> </div> </template> <script> import { GetLogin } from "@/Api/api"; export default { data() { return { form: { userName: "", password: "" }, rules: { userName: [ { required: true, message: "请输入用户名", trigger: "blur" } ], password: [ { required: true, message: "请输入密码", trigger: "blur" } ] } }; }, methods: { submit() { // 调用登录接口 GetLogin(this.form) .then(response => { // 处理登录成功逻辑 }) .catch(error => { // 处理登录失败逻辑 }); } } }; </script> <style lang="less" scoped> .login-container { width: 400px; border: 1px solid #eaeaea; margin: 180px auto; padding: 35px 35px 15px 35px; border-radius: 15px; box-shadow: 0 0 25px #cac6c6; background-color: #ffffff; box-sizing: border-box; } .login_title { text-align: center; margin-bottom: 40px; color: #505458; } .el-input { width: 198px; } .login_button { margin-left: 105px; margin-top: 10px; } </style> ``` 在这个示例中,我们使用了Vue2和Element UI组件库来创建一个登录注册页面。页面包括一个表单,用户可以输入用户名和密码,然后点击登录按钮进行登录操作。 在`<template>`部分,我们使用了Element UI的表单组件和输入框组件来创建表单,其中`v-model`指令用于双向绑定输入框的值。 在`<script>`部分,我们引入了一个封装的axios接口`GetLogin`,它负责处理登录请求。在`submit`方法中,我们调用了`GetLogin`方法来发送登录请求,并根据返回结果进行相应的处理。 在`<style>`部分,我们使用了Less语法来定义页面的样,其中`.login-container`是登录容器的样,`.login_title`是登录标题的样,`.el-input`是输入框的样,`.login_button`是登录按钮的样。这些样通过`scoped`属性进行了作用域限制,只会应用在当前组件中。 希望这个示例对您有所帮助。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nobody.sir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值