当前位置:首页 > VUE

vue实现简单登录功能

2026-02-22 10:17:21VUE

创建Vue项目

使用Vue CLI初始化项目,确保已安装Node.js和Vue CLI。

npm install -g @vue/cli
vue create vue-login-demo
cd vue-login-demo
npm run serve

设计登录表单

src/components下创建Login.vue,包含用户名、密码输入框和提交按钮。

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

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      errorMessage: ''
    };
  },
  methods: {
    handleSubmit() {
      if (this.username === 'admin' && this.password === '123456') {
        alert('登录成功');
        this.$router.push('/dashboard'); // 假设有路由跳转
      } else {
        this.errorMessage = '用户名或密码错误';
      }
    }
  }
};
</script>

<style scoped>
.error { color: red; }
</style>

配置路由

安装Vue Router并配置登录页路由。

npm install vue-router

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

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

const routes = [
  { path: '/login', component: Login },
  // 其他路由...
];

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

export default router;

状态管理(可选)

使用Vuex或Pinia管理登录状态。以Pinia为例:

npm install pinia

创建src/stores/auth.js

import { defineStore } from 'pinia';

export const useAuthStore = defineStore('auth', {
  state: () => ({
    isAuthenticated: false,
    user: null
  }),
  actions: {
    login(username, password) {
      if (username === 'admin' && password === '123456') {
        this.isAuthenticated = true;
        this.user = { username };
        return true;
      }
      return false;
    }
  }
});

Login.vue中调用:

import { useAuthStore } from '../stores/auth';

export default {
  setup() {
    const authStore = useAuthStore();
    const handleSubmit = () => {
      if (authStore.login(username.value, password.value)) {
        router.push('/dashboard');
      }
    };
    return { handleSubmit };
  }
};

表单验证增强

使用Vuelidate或原生验证提升用户体验:

npm install vuelidate

示例规则:

vue实现简单登录功能

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

export default {
  validations: {
    username: { required },
    password: { required, minLength: minLength(6) }
  }
};

安全注意事项

  • 实际项目中需后端验证,避免前端硬编码凭证。
  • 使用HTTPS传输敏感数据。
  • 密码应加密存储(如bcrypt)。

标签: 简单功能
分享给朋友:

相关文章

vue实现桌面功能

vue实现桌面功能

Vue 实现桌面功能的方法 Vue.js 可以通过结合 Electron 或 NW.js 等框架实现桌面应用开发。以下是几种常见的方法: 使用 Vue 与 Electron 结合 Electron…

vue实现考试多选功能

vue实现考试多选功能

Vue实现考试多选功能 数据绑定与选项渲染 使用v-model绑定用户选择的答案数组,结合v-for循环渲染选项列表。示例代码: <template> <div>…

vue的艾特功能实现

vue的艾特功能实现

Vue 中的 @ 功能实现 Vue 中的 @ 符号是 v-on 指令的简写,用于绑定事件监听器。以下是具体实现方式: 基本语法 <button @click="handleClick">…

uniapp实现选项卡功能

uniapp实现选项卡功能

实现选项卡功能的基本方法 在uniapp中实现选项卡功能通常需要使用uni-segmented-control组件或自定义样式结合swiper组件。以下是两种常见实现方式: 使用uni-segmen…

vue实现按钮实现点击功能

vue实现按钮实现点击功能

实现按钮点击功能 在Vue中实现按钮点击功能可以通过v-on指令或@缩写来绑定事件。以下是几种常见的实现方式: 使用v-on指令 通过v-on:click绑定点击事件,触发定义在methods中的方…

vue拨号功能实现

vue拨号功能实现

Vue 拨号功能实现 基本思路 拨号功能通常包括数字键盘、号码显示区、拨号按钮等组件。通过Vue的数据绑定和事件处理机制,可以轻松实现交互逻辑。 实现步骤 1. 创建数字键盘组件 使用Vue的v-f…