当前位置:首页 > 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中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogress…

vue组件实现

vue组件实现

Vue 组件实现基础 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

vue实现颜色

vue实现颜色

Vue 实现颜色的方法 在 Vue 中实现颜色管理可以通过多种方式,以下是一些常见的方法: 使用 CSS 变量动态绑定颜色 在 Vue 组件的样式中定义 CSS 变量,并通过 Vue 的数据绑定动态…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…

vue实现tabs

vue实现tabs

Vue实现Tabs组件的方法 使用动态组件和v-for指令 在Vue中创建Tabs组件可以通过动态组件和v-for指令实现。定义一个包含tab标题和内容的数组,使用v-for渲染tab标题,并通过点击…