当前位置:首页 > VUE

vue 实现登录跳转

2026-01-08 13:17:49VUE

实现登录跳转的核心逻辑

在Vue中实现登录跳转通常涉及路由守卫、状态管理和API交互。以下是具体实现方式:

配置路由守卫

在路由配置文件中添加beforeEach守卫,检查用户是否已登录。未登录时重定向到登录页:

// router/index.js
import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'

Vue.use(Router)

const router = new Router({
  routes: [...]
})

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

创建登录组件

构建包含表单提交逻辑的登录组件,成功登录后跳转到目标页面:

<template>
  <form @submit.prevent="handleLogin">
    <input v-model="username" type="text" placeholder="Username">
    <input v-model="password" type="password" placeholder="Password">
    <button type="submit">Login</button>
  </form>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async handleLogin() {
      try {
        await this.$store.dispatch('login', {
          username: this.username,
          password: this.password
        })
        this.$router.push(this.$route.query.redirect || '/dashboard')
      } catch (error) {
        alert('Login failed')
      }
    }
  }
}
</script>

设置Vuex状态管理

通过Vuex存储登录状态和用户信息:

// store/index.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: null,
    token: null
  },
  getters: {
    isAuthenticated: state => !!state.token
  },
  mutations: {
    SET_USER(state, { user, token }) {
      state.user = user
      state.token = token
    }
  },
  actions: {
    async login({ commit }, credentials) {
      const response = await api.post('/login', credentials)
      commit('SET_USER', response.data)
    }
  }
})

处理带重定向的登录链接

在需要登录的页面跳转时,携带原始路径参数:

// 在需要权限的组件中
this.$router.push({
  path: '/login',
  query: { redirect: this.$route.fullPath }
})

持久化登录状态

使用localStoragecookie保持登录状态:

// 在Vuex mutations中添加持久化逻辑
mutations: {
  SET_USER(state, { user, token }) {
    state.user = user
    state.token = token
    localStorage.setItem('authToken', token)
  },
  INIT_STATE(state) {
    state.token = localStorage.getItem('authToken')
  }
}

// 在应用初始化时调用
store.commit('INIT_STATE')

实现注销功能

清除状态并重定向到登录页:

vue 实现登录跳转

methods: {
  logout() {
    this.$store.commit('SET_USER', { user: null, token: null })
    localStorage.removeItem('authToken')
    this.$router.push('/login')
  }
}

标签: 跳转vue
分享给朋友:

相关文章

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue compile 实现

vue compile 实现

Vue 编译实现原理 Vue 的编译过程将模板字符串转换为渲染函数,主要分为解析、优化和代码生成三个阶段。 解析阶段(Parse) 将模板字符串转换为抽象语法树(AST)。Vue 使用正则表达式和有…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装 v…

vue实现选择

vue实现选择

Vue 实现选择功能的方法 在 Vue 中实现选择功能可以通过多种方式完成,以下介绍几种常见的实现方法。 使用 v-model 绑定单选 通过 v-model 可以轻松实现单选功能。以下是一个简单的…

vue实现123456

vue实现123456

Vue 实现数字 123456 的显示 在 Vue 中显示数字 123456 非常简单,可以通过以下方法实现: 方法一:直接在模板中显示 <template> <div>…