当前位置:首页 > 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中设置路由:

vue实现简单登录功能

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

vue实现简单登录功能

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

示例规则:

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

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

安全注意事项

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

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

相关文章

vue电影功能实现

vue电影功能实现

Vue电影功能实现 数据获取与展示 使用Axios或Fetch API从电影API获取数据,例如TMDB或豆瓣API。在Vue组件的created或mounted生命周期钩子中发起请求,将返回的电影数…

vue实现注册功能

vue实现注册功能

实现注册功能的基本步骤 在Vue中实现注册功能通常需要结合前端表单和后端API交互。以下是常见的实现方式: 创建注册表单组件 <template> <div class="re…

vue实现名字查找功能

vue实现名字查找功能

实现名字查找功能 在Vue中实现名字查找功能,可以通过多种方式完成。以下是几种常见的实现方法: 使用v-model和computed属性 创建一个输入框,使用v-model绑定输入的值,通过comp…

h5能实现的功能

h5能实现的功能

H5能实现的功能 H5(HTML5)是一种现代的网页技术标准,具备丰富的功能和特性,适用于多种场景。以下是H5能够实现的主要功能: 多媒体支持 H5原生支持音频和视频播放,无需依赖Flash等插件。…

vue实现模态功能

vue实现模态功能

Vue 实现模态框功能 方法一:使用组件和v-if/v-show控制显示 创建独立的模态框组件(如Modal.vue),通过props接收标题、内容等数据,使用v-if或v-show控制显示状态。…

vue实现弹窗功能

vue实现弹窗功能

实现基础弹窗组件 在Vue中创建弹窗通常需要定义一个组件,利用v-if或v-show控制显示隐藏。以下是一个基础弹窗组件的实现: <template> <div class="…