当前位置:首页 > VUE

vue.js 实现登录

2026-01-21 16:12:47VUE

使用 Vue.js 实现登录功能

1. 创建 Vue 项目
使用 Vue CLI 初始化项目:

vue create login-app

选择默认配置或手动配置(如 Babel、Router、Vuex 等)。

2. 安装依赖
若需发送 HTTP 请求,安装 Axios:

npm install axios

3. 创建登录组件
src/components/Login.vue 中编写登录表单:

<template>
  <div>
    <form @submit.prevent="handleLogin">
      <input v-model="username" placeholder="用户名" />
      <input v-model="password" type="password" placeholder="密码" />
      <button type="submit">登录</button>
    </form>
    <p v-if="error">{{ error }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: "",
      password: "",
      error: "",
    };
  },
  methods: {
    async handleLogin() {
      try {
        const response = await axios.post("/api/login", {
          username: this.username,
          password: this.password,
        });
        localStorage.setItem("token", response.data.token);
        this.$router.push("/dashboard");
      } catch (err) {
        this.error = "登录失败,请检查用户名或密码";
      }
    },
  },
};
</script>

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

import Vue from "vue";
import Router from "vue-router";
import Login from "../components/Login.vue";

Vue.use(Router);

export default new Router({
  routes: [{ path: "/login", component: Login }],
});

5. 处理 API 请求
src/api/auth.js 中封装登录接口:

import axios from "axios";

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

6. 状态管理(可选)
使用 Vuex 管理用户登录状态:

// src/store/index.js
export default new Vuex.Store({
  state: { user: null, token: null },
  mutations: {
    setUser(state, payload) {
      state.user = payload.user;
      state.token = payload.token;
    },
  },
  actions: {
    async login({ commit }, credentials) {
      const response = await login(credentials);
      commit("setUser", response.data);
    },
  },
});

7. 保护路由
通过路由守卫限制未登录访问:

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

8. 后端对接
确保后端提供 /api/login 接口,返回 JWT 或 session 凭证。

9. 样式优化
添加 CSS 或使用 UI 库(如 Element UI、Vuetify)美化表单。

10. 测试与调试
使用浏览器开发者工具检查网络请求,确保数据传递和状态更新正确。

vue.js 实现登录

关键注意事项

  • 安全性:密码传输需 HTTPS,避免明文存储。
  • Token 存储:使用 localStorageHttpOnly Cookie。
  • 错误处理:显示友好的错误提示,避免暴露敏感信息。

通过以上步骤可实现基础的 Vue.js 登录功能,根据实际需求扩展验证、第三方登录等特性。

标签: vuejs
分享给朋友:

相关文章

vue实现表格多行修改

vue实现表格多行修改

实现多行编辑表格 在Vue中实现表格的多行编辑功能,可以通过以下方法完成。这里以Element UI的表格组件为例,但思路适用于其他UI库或原生实现。 数据准备 定义表格数据和编辑状态管理变量:…

vue 实现菜单

vue 实现菜单

Vue 实现菜单的方法 使用 Vue 实现菜单可以通过多种方式,以下是几种常见的方法: 使用 Vue Router 实现动态路由菜单 通过 Vue Router 可以动态生成菜单,根据路由配置自动渲…

vue评分实现

vue评分实现

Vue 评分组件实现方法 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 引入并注册组件: import Vue from '…

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template> &…

vue实现表白

vue实现表白

Vue 实现表白页面 使用 Vue 可以快速创建一个动态、交互式的表白页面。以下是一个简单的实现方案: 基础结构 创建一个 Vue 项目或单文件组件,包含以下核心部分: <template&…