当前位置:首页 > VUE

vue登录模块实现

2026-02-17 14:09:12VUE

Vue 登录模块实现

创建登录表单组件

使用 Vue 的单文件组件结构,构建包含用户名和密码输入框的登录表单。表单需绑定 v-model 实现双向数据绑定,并添加提交事件处理。

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

<script>
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    handleLogin() {
      // 登录逻辑
    },
  },
};
</script>

表单验证

引入 Vuelidate 或手动验证,确保输入符合要求(如非空、长度限制等)。

vue登录模块实现

import { required, minLength } from "vuelidate/lib/validators";

export default {
  validations: {
    form: {
      username: { required, minLength: minLength(3) },
      password: { required, minLength: minLength(6) },
    },
  },
  methods: {
    handleLogin() {
      this.$v.$touch();
      if (!this.$v.$invalid) {
        // 提交逻辑
      }
    },
  },
};

调用登录 API

使用 axios 发送登录请求,处理成功或失败的回调。

methods: {
  async handleLogin() {
    try {
      const response = await axios.post("/api/login", this.form);
      localStorage.setItem("token", response.data.token);
      this.$router.push("/dashboard");
    } catch (error) {
      alert("登录失败: " + error.response.data.message);
    }
  },
},

路由守卫配置

在 Vue Router 中配置全局前置守卫,限制未登录用户访问受保护路由。

vue登录模块实现

router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem("token");
  if (to.matched.some(record => record.meta.requiresAuth) && !isAuthenticated) {
    next("/login");
  } else {
    next();
  }
});

状态管理(可选)

使用 Vuex 集中管理用户登录状态,便于跨组件共享数据。

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

响应式布局与样式

添加基础样式确保表单在不同设备上显示正常。

.login-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
}
input {
  width: 100%;
  padding: 10px;
  margin: 10px 0;
}
button {
  width: 100%;
  padding: 10px;
  background: #42b983;
  color: white;
  border: none;
}

标签: 模块vue
分享给朋友:

相关文章

vue实现好友列表

vue实现好友列表

实现好友列表的Vue组件 数据准备 在Vue组件的data中定义好友列表数据,通常是一个数组对象,每个对象包含好友的基本信息如ID、头像、昵称等。 data() { return { f…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…

利用vue 实现

利用vue 实现

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

vue 实现fadeout

vue 实现fadeout

Vue 实现淡出效果 在 Vue 中实现淡出(fade-out)效果可以通过 CSS 过渡或动画结合 Vue 的动态渲染功能完成。以下是几种常见方法: 使用 Vue 过渡(Transition)组件…

vue 实现长按

vue 实现长按

实现长按功能的基本思路 在Vue中实现长按功能,通常需要监听元素的mousedown或touchstart事件,启动一个计时器。当用户按住元素超过设定的时间阈值时,触发长按回调函数。如果在时间阈值内触…

vue实现字幕

vue实现字幕

Vue 实现字幕功能 在 Vue 中实现字幕功能可以通过动态绑定数据和 CSS 动画来实现。以下是具体实现方法: 1. 基础字幕组件 创建一个字幕组件,通过 v-model 或 props 接收字…