当前位置:首页 > VUE

vue 实现登录退出

2026-01-15 02:58:18VUE

Vue 实现登录和退出功能

安装依赖

确保项目已安装 Vue 和 Vue Router。若未安装,可通过以下命令安装:

npm install vue vue-router

创建登录组件

src/components 目录下创建 Login.vue 文件,实现登录表单和逻辑:

<template>
  <div>
    <input v-model="username" placeholder="用户名">
    <input v-model="password" type="password" placeholder="密码">
    <button @click="handleLogin">登录</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    handleLogin() {
      if (this.username && this.password) {
        localStorage.setItem('token', '模拟token')
        this.$router.push('/')
      }
    }
  }
}
</script>

创建路由配置

src/router/index.js 中配置登录路由和主页路由:

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

Vue.use(Router)

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

实现路由守卫

src/main.js 或路由配置中添加全局前置守卫,检查用户是否登录:

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

实现退出功能

在主页组件 Home.vue 中添加退出按钮和逻辑:

<template>
  <div>
    <h1>欢迎</h1>
    <button @click="handleLogout">退出</button>
  </div>
</template>

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

状态管理(可选)

对于复杂应用,建议使用 Vuex 管理登录状态。创建 src/store/index.js

vue 实现登录退出

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

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: localStorage.getItem('token') || null
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      localStorage.setItem('token', token)
    },
    clearToken(state) {
      state.token = null
      localStorage.removeItem('token')
    }
  }
})

完整流程

  1. 用户访问受保护路由时,路由守卫检查本地存储中的 token
  2. 未登录则跳转到登录页面
  3. 登录成功后存储 token 并跳转到主页
  4. 退出时清除 token 并返回登录页

注意事项

  • 实际项目中应使用 HTTPS 加密通信
  • 敏感信息不应长期存储在 localStorage 中
  • 后端需实现真正的身份验证接口
  • 考虑添加 token 过期机制

标签: vue
分享给朋友:

相关文章

django vue实现

django vue实现

Django与Vue.js整合实现方案 Django作为后端框架与Vue.js作为前端框架的整合,可以通过以下方式实现: 分离式开发 前后端完全分离,Django仅提供API接口,Vue.js通过a…

vue 实现闪烁

vue 实现闪烁

Vue 实现闪烁效果的方法 方法一:使用 CSS 动画 在 Vue 组件中定义一个闪烁的 CSS 动画,通过绑定 class 或 style 来控制元素的闪烁效果。 <template>…

vue实现webapp

vue实现webapp

Vue 实现 WebApp 的基本步骤 环境准备 确保已安装 Node.js 和 npm/yarn。通过 Vue CLI 或 Vite 创建项目: npm install -g @vue/cli v…

vue 实现excel

vue 实现excel

Vue 实现 Excel 功能 前端导出 Excel 使用 xlsx 库可以轻松实现前端 Excel 导出功能。安装依赖: npm install xlsx 导出 Excel 示例代码: impo…

vue实现白板

vue实现白板

Vue实现白板功能 使用Vue实现白板功能可以通过HTML5的Canvas API结合Vue的响应式特性来完成。以下是实现白板功能的关键步骤: 安装依赖 在Vue项目中安装必要的依赖,如vue-dr…

vue实现抽屉

vue实现抽屉

Vue 实现抽屉组件 抽屉组件是一种常见的 UI 模式,通常用于从屏幕边缘滑出内容。以下是几种实现抽屉的方法: 使用 Vue 原生实现 创建一个基本的抽屉组件,利用 Vue 的过渡和条件渲染功能。…