当前位置:首页 > VUE

vue 实现简单登陆

2026-03-27 01:52:41VUE

创建Vue项目

使用Vue CLI初始化项目:

vue create vue-login-demo

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

安装必要依赖

若需使用Axios发送请求,安装它:

vue 实现简单登陆

npm install axios

设计登录组件

src/components/Login.vue中创建登录表单:

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

<script>
import axios from 'axios';

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

<style scoped>
.login {
  max-width: 300px;
  margin: 0 auto;
  padding: 20px;
}
.error {
  color: red;
}
</style>

配置路由

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

vue 实现简单登陆

import { createRouter, createWebHistory } from 'vue-router';
import Login from '../components/Login.vue';
import Dashboard from '../components/Dashboard.vue';

const routes = [
  { path: '/login', component: Login },
  { path: '/dashboard', component: Dashboard, meta: { requiresAuth: true } }
];

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

router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth && !localStorage.getItem('token')) {
    next('/login');
  } else {
    next();
  }
});

export default router;

添加Dashboard组件

src/components/Dashboard.vue中创建受保护页面:

<template>
  <div>
    <h3>欢迎进入仪表盘</h3>
    <button @click="handleLogout">退出登录</button>
  </div>
</template>

<script>
export default {
  methods: {
    handleLogout() {
      localStorage.removeItem('token');
      this.$router.push('/login');
    }
  }
};
</script>

运行项目

启动开发服务器:

npm run serve

关键点说明

  • 表单使用v-model双向绑定数据。
  • @submit.prevent阻止默认表单提交行为。
  • 通过Axios发送POST请求到模拟API(需替换为真实后端地址)。
  • 使用localStorage存储登录令牌(Token)。
  • 路由守卫检查认证状态,未登录用户跳转到登录页。

注意事项

  • 实际项目中应使用HTTPS和安全存储(如Vuex或Pinia管理状态)。
  • 密码需加密传输,后端需验证并返回安全的Token。
  • 可添加表单验证库(如VeeValidate)增强输入校验。

标签: 简单vue
分享给朋友:

相关文章

vue实现钟表

vue实现钟表

使用 Vue 实现动态钟表 以下是一个基于 Vue 3 的动态钟表实现方案,包含时分秒指针和数字时间显示。 模板部分 <template> <div class="cloc…

vue实现route

vue实现route

Vue 路由实现方法 Vue 中实现路由通常使用 vue-router 库,这是 Vue 官方推荐的路由管理器。以下是具体实现步骤: 安装 vue-router 通过 npm 或 yarn 安装…

利用vue 实现

利用vue 实现

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

vue 实现评分

vue 实现评分

Vue 实现评分功能 在 Vue 中实现评分功能可以通过多种方式完成,包括使用第三方组件库或自定义实现。以下是两种常见的方法: 使用第三方组件库(如 Element UI) Element UI 提…

vue  全局刷新实现

vue 全局刷新实现

全局刷新实现方法 在Vue中实现全局刷新通常涉及重新加载整个应用或特定路由。以下是几种常见方法: 使用window.location.reload() 强制浏览器重新加载当前页面: window.…

vue修改功能实现

vue修改功能实现

Vue 修改功能的实现方法 在 Vue 中实现修改功能通常涉及表单绑定、数据更新和事件处理。以下是几种常见的实现方式: 数据双向绑定 使用 v-model 指令实现表单元素与 Vue 实例数据的双向…