当前位置:首页 > 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 中的实现方法 使用 HTML5 原生拖放 API Vue 可以结合 HTML5 的拖放 API 实现基础拖拽功能。通过 draggable 属性标记可拖拽元素,监听 dragst…

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独…

ssh vue 实现

ssh vue 实现

使用SSH连接Vue项目部署的服务器 在Vue项目开发或部署过程中,可能需要通过SSH连接到远程服务器进行配置或维护。以下是实现SSH连接并部署Vue项目的关键步骤: 生成SSH密钥对 在本地终端运…

vue实现报表

vue实现报表

vue实现报表的方法 使用Vue实现报表通常需要结合图表库或表格组件。以下是几种常见方法: 使用ECharts 安装ECharts库: npm install echarts vue-echa…

vue 实现直播

vue 实现直播

Vue 实现直播的基本方法 在Vue中实现直播功能,通常需要结合WebRTC、RTMP或HLS等技术。以下是几种常见的实现方式: 使用WebRTC实现实时直播 WebRTC适合低延迟的实时直播场景,…

vue实现oauth

vue实现oauth

Vue 实现 OAuth 的步骤 安装必要的依赖 确保项目中安装了 axios 和 vue-router。OAuth 通常需要发送 HTTP 请求和处理路由跳转。 npm install axio…