当前位置:首页 > VUE

vue实现登录注册6

2026-02-24 08:35:22VUE

以下是一个基于Vue 3的登录注册实现方案,包含核心功能模块和代码示例:

路由配置

router/index.js中配置登录和注册路由:

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  }
]

登录组件

创建Login.vue文件:

vue实现登录注册6

<template>
  <form @submit.prevent="handleLogin">
    <input v-model="email" type="email" placeholder="Email" required>
    <input v-model="password" type="password" placeholder="Password" required>
    <button type="submit">Login</button>
  </form>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const email = ref('')
const password = ref('')
const router = useRouter()

const handleLogin = async () => {
  try {
    // 这里替换为实际的API调用
    const response = await fakeAuthAPI({ email, password })
    localStorage.setItem('token', response.token)
    router.push('/dashboard')
  } catch (error) {
    alert(error.message)
  }
}
</script>

注册组件

创建Register.vue文件:

<template>
  <form @submit.prevent="handleRegister">
    <input v-model="name" placeholder="Full Name" required>
    <input v-model="email" type="email" placeholder="Email" required>
    <input v-model="password" type="password" placeholder="Password" required>
    <input v-model="confirmPassword" type="password" placeholder="Confirm Password" required>
    <button type="submit">Register</button>
  </form>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const name = ref('')
const email = ref('')
const password = ref('')
const confirmPassword = ref('')
const router = useRouter()

const handleRegister = async () => {
  if (password.value !== confirmPassword.value) {
    alert("Passwords don't match")
    return
  }

  try {
    // 这里替换为实际的API调用
    await fakeRegisterAPI({ name, email, password })
    router.push('/login')
  } catch (error) {
    alert(error.message)
  }
}
</script>

状态管理

创建authStore.js使用Pinia:

vue实现登录注册6

import { defineStore } from 'pinia'

export const useAuthStore = defineStore('auth', {
  state: () => ({
    user: null,
    isAuthenticated: false
  }),
  actions: {
    setUser(user) {
      this.user = user
      this.isAuthenticated = true
    },
    logout() {
      this.user = null
      this.isAuthenticated = false
      localStorage.removeItem('token')
    }
  }
})

路由守卫

在路由配置中添加全局前置守卫:

router.beforeEach((to) => {
  const isAuthenticated = localStorage.getItem('token')
  if (to.name !== 'Login' && to.name !== 'Register' && !isAuthenticated) {
    return { name: 'Login' }
  }
})

API模拟函数

创建api/auth.js模拟后端接口:

export const fakeAuthAPI = ({ email, password }) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (email === 'test@example.com' && password === 'password') {
        resolve({ token: 'fake-jwt-token' })
      } else {
        reject(new Error('Invalid credentials'))
      }
    }, 500)
  })
}

export const fakeRegisterAPI = ({ name, email, password }) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({ success: true })
    }, 500)
  })
}

该实现包含表单验证、状态管理、路由保护和基本的错误处理。实际项目中需要替换模拟API为真实的后端接口调用,并添加更完善的验证逻辑和安全措施。

标签: vue
分享给朋友:

相关文章

vue交互实现

vue交互实现

Vue 交互实现方法 Vue.js 提供了多种方式实现用户交互,包括事件处理、表单绑定、动态渲染等。以下是常见的交互实现方法: 事件处理 通过 v-on 或 @ 指令绑定事件,触发方法或直接执行表达…

vue实现新闻

vue实现新闻

Vue 实现新闻功能 使用 Vue 实现新闻功能通常涉及新闻列表展示、新闻详情页、分类筛选等模块。以下是实现的基本思路和代码示例。 新闻列表展示 通过 Vue 组件展示新闻列表,通常使用 v-for…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…

vue 实现href

vue 实现href

Vue 实现 href 的方法 在 Vue 中实现 href 功能可以通过多种方式,具体取决于需求场景。以下是几种常见的方法: 使用 <a> 标签 直接使用 HTML 的 <a&g…

vue 实现loading

vue 实现loading

Vue 实现 Loading 的方法 使用 v-if 和 v-show 控制显示 在 Vue 中可以通过 v-if 或 v-show 控制 loading 组件的显示与隐藏。v-if 会动态创建或销毁…

vue 实现图片

vue 实现图片

Vue 实现图片加载与展示的方法 在 Vue 中实现图片加载和展示可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind 绑定图片路径 通过 v-bind(或简写为 :)动态绑定图片的…