当前位置:首页 > VUE

vue实现登录框

2026-02-17 19:58:49VUE

实现登录框的基本结构

在Vue中创建一个登录框,需要使用<template>定义HTML结构,<script>处理逻辑,<style>添加样式。以下是一个基础登录框的模板:

<template>
  <div class="login-container">
    <form @submit.prevent="handleSubmit">
      <h2>用户登录</h2>
      <div class="form-group">
        <label for="username">用户名</label>
        <input 
          type="text" 
          id="username" 
          v-model="username" 
          placeholder="请输入用户名"
          required
        />
      </div>
      <div class="form-group">
        <label for="password">密码</label>
        <input 
          type="password" 
          id="password" 
          v-model="password" 
          placeholder="请输入密码"
          required
        />
      </div>
      <button type="submit">登录</button>
    </form>
  </div>
</template>

数据绑定与事件处理

<script>中定义数据模型和提交方法,使用v-model实现双向数据绑定:

vue实现登录框

<script>
export default {
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    handleSubmit() {
      // 这里替换为实际的登录逻辑
      console.log('提交数据:', {
        username: this.username,
        password: this.password
      });
      // 示例:调用API
      // this.$axios.post('/login', {
      //   username: this.username,
      //   password: this.password
      // })
    }
  }
}
</script>

样式优化

通过<style>添加基础样式,建议使用scoped避免样式污染:

vue实现登录框

<style scoped>
.login-container {
  max-width: 400px;
  margin: 50px auto;
  padding: 20px;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.form-group {
  margin-bottom: 15px;
}
label {
  display: block;
  margin-bottom: 5px;
}
input {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
}
button {
  width: 100%;
  padding: 10px;
  background-color: #42b983;
  color: white;
  border: none;
}
</style>

表单验证增强

添加即时验证功能,使用Vuelidate或原生HTML5验证:

import { required, minLength } from 'vuelidate/lib/validators'

export default {
  data() {
    return { username: '', password: '' }
  },
  validations: {
    username: { required },
    password: { required, minLength: minLength(6) }
  },
  methods: {
    handleSubmit() {
      if (this.$v.$invalid) {
        alert('请检查表单输入')
        return
      }
      // 提交逻辑
    }
  }
}

完整组件示例

整合所有部分后的完整组件代码:

<template>
  <div class="login-container">
    <form @submit.prevent="handleSubmit">
      <h2>用户登录</h2>
      <div class="form-group">
        <label for="username">用户名</label>
        <input 
          type="text" 
          v-model="username"
          @blur="$v.username.$touch()"
          :class="{ 'error': $v.username.$error }"
        >
        <p v-if="$v.username.$error" class="error-message">必须填写用户名</p>
      </div>
      <div class="form-group">
        <label for="password">密码</label>
        <input 
          type="password" 
          v-model="password"
          @blur="$v.password.$touch()"
          :class="{ 'error': $v.password.$error }"
        >
        <p v-if="$v.password.$error" class="error-message">密码至少6位</p>
      </div>
      <button type="submit" :disabled="$v.$invalid">登录</button>
    </form>
  </div>
</template>

<script>
import { required, minLength } from 'vuelidate/lib/validators'

export default {
  data() {
    return {
      username: '',
      password: ''
    }
  },
  validations: {
    username: { required },
    password: { required, minLength: minLength(6) }
  },
  methods: {
    handleSubmit() {
      // 实际登录逻辑
      console.log('登录数据:', this.username, this.password)
    }
  }
}
</script>

<style scoped>
.error {
  border-color: red;
}
.error-message {
  color: red;
  font-size: 0.8em;
}
</style>

标签: vue
分享给朋友:

相关文章

vue 实现复选

vue 实现复选

Vue 实现复选功能 在 Vue 中实现复选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以自动收集用户选择的复选框值。…

vue实现摘要

vue实现摘要

Vue 实现摘要的方法 在 Vue 中实现文本摘要功能通常涉及截取文本的前部分内容并添加省略号。可以通过计算属性、过滤器或自定义指令来实现。 计算属性实现 在 Vue 组件中定义一个计算属性,用于截…

vue 实现grid

vue 实现grid

在Vue中实现Grid布局可以通过多种方式完成,以下是几种常见的方法: 使用CSS Grid布局 CSS Grid是一种强大的布局系统,可以直接在Vue组件的样式中使用。以下是一个简单的示例:…

vue菜单实现

vue菜单实现

Vue 菜单实现方法 使用 Element UI 的菜单组件 Element UI 提供了现成的菜单组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用 el-menu 相关组件。…

vue实现返回

vue实现返回

返回按钮功能实现 在Vue中实现返回功能通常有两种方式:使用浏览器历史记录的history.back()或结合Vue Router进行编程式导航。以下是具体实现方法: 使用浏览器API metho…

vue实现复制

vue实现复制

Vue 实现复制功能 在 Vue 中实现复制功能可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 Clipboard API Clipboard API 是现代浏览器提供的一种原生 AP…