当前位置:首页 > 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实现选区

vue实现选区

Vue 实现选区的基本方法 在Vue中实现选区功能通常涉及DOM操作和事件处理。以下是几种常见的方法: 使用原生JavaScript的Selection API 通过window.getSelec…

vue实现flvvideo

vue实现flvvideo

vue-flv-player 实现 FLV 视频播放 安装依赖包 npm install flv.js vue-flv-player 基础组件引入 <template> <v…

vue cli 实现

vue cli 实现

Vue CLI 实现步骤 安装 Vue CLI 确保 Node.js 版本在 8.9 或以上。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 创建新项目 使…

vue实现加减

vue实现加减

Vue 实现加减功能 在 Vue 中实现加减功能可以通过数据绑定和事件监听来完成。以下是一个简单的实现方法: 模板部分 <template> <div> <…

vue实现微云

vue实现微云

Vue 实现微云功能 使用 Vue 实现类似微云的网盘功能需要结合前端框架、后端存储及文件处理技术。以下是关键实现步骤和代码示例: 前端框架搭建 使用 Vue CLI 创建项目基础结构,安装必要依赖…