当前位置:首页 > VUE

前端vue实现登录页面

2026-02-24 07:18:58VUE

Vue 登录页面实现步骤

项目初始化与依赖安装

使用 Vue CLI 创建项目并安装必要依赖(如 Vue Router、Axios):

vue create login-demo
cd login-demo
npm install vue-router axios

路由配置

src/router/index.js 中配置登录路由:

import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router

登录组件实现

创建 src/views/Login.vue 文件:

前端vue实现登录页面

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

<script>
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

export default {
  setup() {
    const router = useRouter()
    const form = reactive({
      username: '',
      password: ''
    })

    const handleSubmit = async () => {
      try {
        const response = await axios.post('/api/login', form)
        localStorage.setItem('token', response.data.token)
        router.push('/dashboard')
      } catch (error) {
        alert('登录失败: ' + error.response.data.message)
      }
    }

    return { form, handleSubmit }
  }
}
</script>

<style scoped>
.login-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
}
.form-group {
  margin-bottom: 15px;
}
input {
  width: 100%;
  padding: 8px;
  box-sizing: border-box;
}
button {
  width: 100%;
  padding: 10px;
  background-color: #42b983;
  color: white;
  border: none;
  cursor: pointer;
}
</style>

表单验证增强

使用 Vuelidate 进行表单验证(需安装 vuelidate):

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

validations: {
  form: {
    username: { required },
    password: { required, minLength: minLength(6) }
  }
}

API 请求封装

创建 src/api/auth.js 封装登录请求:

前端vue实现登录页面

import axios from 'axios'

export const login = (credentials) => {
  return axios.post('/api/login', credentials)
}

响应式布局优化

添加媒体查询适应移动端:

@media (max-width: 600px) {
  .login-container {
    padding: 10px;
    width: 90%;
  }
}

安全增强措施

实现以下安全措施:

  • 密码输入框添加显示/隐藏切换功能
  • 提交按钮添加防重复点击处理
  • CSRF Token 防护(根据后端框架配置)

状态管理整合

如需全局管理用户状态,可结合 Vuex:

// store/modules/auth.js
const actions = {
  async login({ commit }, credentials) {
    const response = await login(credentials)
    commit('SET_USER', response.data.user)
    return response
  }
}

以上实现包含基础登录功能、表单验证、API交互和样式布局,可根据实际项目需求扩展更多功能如第三方登录、密码找回等。

标签: 页面vue
分享给朋友:

相关文章

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

vue 实现回复

vue 实现回复

Vue 实现回复功能 在 Vue 中实现回复功能通常涉及表单提交、数据绑定和列表渲染。以下是实现步骤和代码示例: 数据绑定与表单 <template> <div>…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

vue监听实现

vue监听实现

Vue 监听实现方法 在 Vue 中可以通过多种方式实现监听数据变化或事件触发,以下是常见的几种方法: 使用 watch 选项 watch 是 Vue 提供的核心功能之一,用于观察和响应 Vue 实…

vue实现影院

vue实现影院

Vue实现影院功能的基本思路 使用Vue框架开发影院功能需要结合前端组件化开发、状态管理以及后端API交互。以下是关键实现步骤: 影院页面布局设计 影院页面通常分为顶部导航栏、影院列表区域和底部导航…

vue实现watch

vue实现watch

监听数据变化 在Vue中,watch用于监听响应式数据的变化并执行回调函数。基本语法如下: watch: { propertyName(newValue, oldValue) { //…