当前位置:首页 > 前端教程

login elementui

2026-03-06 02:12:29前端教程

使用 Element UI 实现登录功能

Element UI 提供了丰富的表单组件,可以快速构建登录界面。以下是一个完整的登录表单实现示例:

安装 Element UI 确保项目中已安装 Element UI。可以通过 npm 或 yarn 安装:

npm install element-ui

引入 Element UI 在 main.js 中全局引入 Element UI:

import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(ElementUI)

创建登录组件 创建一个 Login.vue 组件,包含表单验证功能:

<template>
  <div class="login-container">
    <el-form :model="loginForm" :rules="rules" ref="loginForm" class="login-form">
      <h3 class="title">系统登录</h3>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          placeholder="用户名"
          prefix-icon="el-icon-user"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          placeholder="密码"
          prefix-icon="el-icon-lock"
          @keyup.enter.native="handleLogin"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :loading="loading"
          @click.native.prevent="handleLogin"
          style="width:100%"
        >登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          // 调用登录接口
          this.$axios.post('/login', this.loginForm)
            .then(response => {
              this.loading = false
              // 处理登录成功逻辑
              this.$router.push('/dashboard')
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f0f2f5;
}

.login-form {
  width: 350px;
  padding: 25px 25px 5px 25px;
  background: #fff;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0,0,0,.1);
}

.title {
  margin: 0 auto 30px;
  color: #707070;
  text-align: center;
}
</style>

表单验证配置

Element UI 的表单验证通过 rules 属性配置。上述示例中定义了基本的必填验证:

rules: {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

登录请求处理

登录按钮点击时会触发 handleLogin 方法,该方法会先验证表单,验证通过后发送登录请求:

handleLogin() {
  this.$refs.loginForm.validate(valid => {
    if (valid) {
      this.loading = true
      this.$axios.post('/api/login', this.loginForm)
        .then(response => {
          // 存储token
          localStorage.setItem('token', response.data.token)
          this.$router.push('/dashboard')
        })
        .catch(error => {
          this.$message.error(error.response.data.message)
        })
        .finally(() => {
          this.loading = false
        })
    }
  })
}

样式优化

登录页面通常需要全屏居中显示,可以通过以下 CSS 实现:

.login-container {
  position: fixed;
  width: 100%;
  height: 100%;
  background: url('~@/assets/login-bg.jpg') no-repeat center;
  background-size: cover;
}

.login-form {
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.9);
  width: 400px;
  padding: 25px 25px 15px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
}

响应式处理

针对不同屏幕尺寸可以添加媒体查询:

login elementui

@media (max-width: 768px) {
  .login-form {
    width: 90%;
    margin: 0 auto;
  }
}

标签: loginelementui
分享给朋友:

相关文章

vue实现login

vue实现login

Vue 实现登录功能 在 Vue 中实现登录功能通常需要以下几个步骤: 创建登录表单组件 在 Vue 项目中创建一个登录表单组件,包含用户名和密码输入框以及提交按钮。可以使用 Vue 的模板语法和双…

elementui中文网

elementui中文网

Element UI 中文网相关信息 Element UI 是一款基于 Vue.js 的开源 UI 组件库,由饿了么前端团队开发和维护。以下是关于 Element UI 中文网的相关信息: 官方网站…

elementui datepick

elementui datepick

ElementUI DatePicker 使用指南 ElementUI 是一套基于 Vue.js 的组件库,DatePicker 是其中一个常用的日期选择组件。以下是关于 ElementUI Date…

elementui渐变

elementui渐变

实现ElementUI渐变效果的方法 ElementUI组件库本身不直接提供渐变样式,但可以通过CSS自定义实现渐变效果。以下是几种常见实现方式: 背景渐变 通过CSS的linear-gradien…

elementui 2.0

elementui 2.0

Element UI 2.0 是 Vue.js 的一个流行组件库版本,发布于 2018 年,提供了丰富的 UI 组件和工具,适用于快速构建企业级中后台产品。以下是关于 Element UI 2.0 的…

elementui selecttree

elementui selecttree

使用 ElementUI 的 TreeSelect 组件 ElementUI 提供了 el-tree-select 组件,用于结合树形结构和下拉选择功能。以下是其基本用法和常见配置。 基本使用…