当前位置:首页 > VUE

vue实现登录框

2026-03-29 20:13:05VUE

创建Vue登录组件

使用Vue CLI或手动创建Login.vue组件文件,包含表单基本结构:

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

设置数据绑定与验证

在script部分设置响应式数据和基础验证:

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      errors: {}
    }
  },
  methods: {
    validateForm() {
      this.errors = {}
      if (!this.form.username) this.errors.username = '请输入用户名'
      if (!this.form.password) this.errors.password = '请输入密码'
      return Object.keys(this.errors).length === 0
    }
  }
}
</script>

实现登录逻辑

添加提交方法和API调用:

<script>
import axios from 'axios'

export default {
  methods: {
    async handleSubmit() {
      if (!this.validateForm()) return

      try {
        const response = await axios.post('/api/login', this.form)
        localStorage.setItem('token', response.data.token)
        this.$router.push('/dashboard')
      } catch (error) {
        this.errors.api = error.response?.data?.message || '登录失败'
      }
    }
  }
}
</script>

添加样式增强体验

在style部分添加基础样式:

<style scoped>
.login-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
}
.form-group {
  margin-bottom: 15px;
}
label {
  display: block;
  margin-bottom: 5px;
}
input {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
}
button {
  background: #42b983;
  color: white;
  padding: 10px 15px;
  border: none;
  cursor: pointer;
}
.error {
  color: red;
  font-size: 12px;
}
</style>

添加状态管理与路由保护

在Vuex中设置登录状态管理:

// store.js
export default new Vuex.Store({
  state: {
    isAuthenticated: false,
    user: null
  },
  mutations: {
    SET_AUTH(state, payload) {
      state.isAuthenticated = true
      state.user = payload.user
    }
  }
})

添加路由导航守卫:

vue实现登录框

// router.js
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!store.state.isAuthenticated) {
      next({ path: '/login' })
    } else {
      next()
    }
  } else {
    next()
  }
})

标签: vue
分享给朋友:

相关文章

利用vue 实现

利用vue 实现

以下是利用 Vue 实现常见功能的几种方法,分为不同场景和技术要点: 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现双向绑定: <template>…

js 实现vue

js 实现vue

实现 Vue 的核心功能 在 JavaScript 中实现 Vue 的核心功能需要模拟数据绑定、虚拟 DOM 和响应式系统。以下是一个简化版的实现思路: 响应式系统 通过 Object.define…

vue实现伸缩

vue实现伸缩

Vue实现伸缩功能的方法 使用CSS过渡和Vue的v-if或v-show 通过CSS的transition属性结合Vue的条件渲染指令(v-if或v-show)可以实现元素的伸缩效果。定义一个CSS类…

vue实现双击

vue实现双击

Vue 实现双击事件的方法 在Vue中实现双击事件可以通过以下几种方式完成,具体选择取决于项目需求和开发习惯。 使用 @dblclick 指令 Vue提供了内置的@dblclick指令,可以…

vue 实现table

vue 实现table

Vue 实现 Table 的方法 使用原生 HTML 表格 通过 Vue 的 v-for 指令动态渲染表格数据,适合简单表格场景。 <template> <table>…

vue下载实现

vue下载实现

Vue 下载实现方法 在 Vue 项目中实现文件下载功能可以通过多种方式完成,以下是几种常见的方法: 使用 <a> 标签实现下载 通过动态创建 <a> 标签并设置 href…