当前位置:首页 > VUE

vue 实现简单登陆

2026-01-12 04:07:00VUE

Vue 实现简单登录功能

创建 Vue 项目

使用 Vue CLI 创建一个新项目:

vue create login-demo

选择默认配置或手动配置(推荐包含 Vue Router 和 Vuex)。

项目结构

src/
├── components/
│   └── LoginForm.vue
├── views/
│   ├── Login.vue
│   └── Home.vue
├── router/
│   └── index.js
├── store/
│   └── index.js
└── App.vue

登录表单组件

components/LoginForm.vue 中:

<template>
  <form @submit.prevent="handleSubmit">
    <input v-model="username" placeholder="用户名" required>
    <input v-model="password" type="password" placeholder="密码" required>
    <button type="submit">登录</button>
  </form>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    handleSubmit() {
      this.$emit('login', {
        username: this.username,
        password: this.password
      })
    }
  }
}
</script>

登录页面

views/Login.vue 中:

<template>
  <div>
    <h1>登录</h1>
    <LoginForm @login="handleLogin" />
    <p v-if="error" class="error">{{ error }}</p>
  </div>
</template>

<script>
import LoginForm from '@/components/LoginForm.vue'

export default {
  components: { LoginForm },
  data() {
    return {
      error: null
    }
  },
  methods: {
    handleLogin(credentials) {
      this.$store.dispatch('login', credentials)
        .then(() => {
          this.$router.push('/home')
        })
        .catch(error => {
          this.error = error.message
        })
    }
  }
}
</script>

Vuex 状态管理

store/index.js 中:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isAuthenticated: false,
    user: null
  },
  mutations: {
    SET_AUTH(state, payload) {
      state.isAuthenticated = true
      state.user = payload.user
    }
  },
  actions: {
    login({ commit }, credentials) {
      return new Promise((resolve, reject) => {
        // 模拟 API 调用
        if (credentials.username === 'admin' && credentials.password === '123456') {
          commit('SET_AUTH', { user: credentials.username })
          resolve()
        } else {
          reject(new Error('用户名或密码错误'))
        }
      })
    }
  }
})

路由配置

router/index.js 中:

import Vue from 'vue'
import Router from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      meta: { requiresAuth: true }
    },
    {
      path: '*',
      redirect: '/login'
    }
  ]
})

路由守卫

main.js 中添加全局前置守卫:

import router from './router'
import store from './store'

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!store.state.isAuthenticated) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})

主页面

views/Home.vue 中:

<template>
  <div>
    <h1>欢迎, {{ $store.state.user }}</h1>
    <button @click="logout">退出</button>
  </div>
</template>

<script>
export default {
  methods: {
    logout() {
      this.$store.commit('SET_AUTH', { user: null })
      this.$store.state.isAuthenticated = false
      this.$router.push('/login')
    }
  }
}
</script>

样式增强

可以添加基础样式到 App.vue

<style>
.error {
  color: red;
}
input {
  display: block;
  margin: 10px 0;
  padding: 8px;
}
button {
  padding: 8px 16px;
}
</style>

运行项目

npm run serve

访问 http://localhost:8080/login 测试登录功能,使用用户名 admin 和密码 123456 可以成功登录并跳转到主页。

vue 实现简单登陆

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

相关文章

vue实现评论编辑

vue实现评论编辑

Vue 实现评论编辑功能 数据绑定与表单结构 使用 v-model 双向绑定评论内容到表单输入框,确保编辑时能显示原内容并同步修改。示例模板结构: <template> <d…

vue实现视频开场

vue实现视频开场

使用 Vue 实现视频开场动画 在 Vue 中实现视频开场动画可以通过结合 HTML5 的 <video> 标签和 Vue 的过渡效果或生命周期钩子来完成。以下是具体实现方法: 安装依…

vue实现路由导航

vue实现路由导航

路由导航的实现方式 在Vue中,路由导航主要通过Vue Router实现。Vue Router是Vue.js官方的路由管理器,用于构建单页面应用(SPA)。 基本路由配置 安装Vue Router后…

vue实现好评弹框

vue实现好评弹框

Vue 实现好评弹框的方法 使用 Vue 实现好评弹框可以通过组件化方式完成,结合动态数据绑定和事件处理。以下是具体实现步骤: 1. 创建弹框组件 新建一个 RatingDialog.vue 组件…

vue页面分离的实现

vue页面分离的实现

Vue页面分离的实现方法 将Vue页面分离为多个组件或模块,有助于提升代码可维护性和复用性。以下是几种常见的实现方式: 组件化拆分 通过将页面拆分为多个子组件,每个组件负责特定功能或UI部分。使用…

vue如何实现放大缩小

vue如何实现放大缩小

Vue实现放大缩小功能的方法 在Vue中实现元素的放大缩小功能,可以通过CSS变换、第三方库或直接操作DOM样式来实现。以下是几种常见的方法: 使用CSS transform属性 通过绑定Vue的d…