Vue router 全局路由守衛


  記錄一下全局路由守衛的使用;

  方法一:定義一個數組用於檢測與管理需要登錄的頁面,全局路由守衛配合本地存儲判斷是否跳轉

import Vue from 'vue'
import Router from 'vue-router'
import store from './../store'

import Home from 'components/home/home' // 主頁組件

// 其它組件...

import Cart from 'components/cart/cart' // 購物車組件
import User from 'components/user/user' // 用戶中心組件

// 其他組件...

import GoodsDetail from 'components/goods-detail/goods-detail' // 商品詳情組件

import { localTake } from 'common/js/localStore' // 本地存儲方法封裝

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/', // 默認地址
      redirect: '/home'
    },
    {
      path: '/home',
      component: Home,
      name: 'Home',
      meta: {
        title: '主頁',
        keepAlive: true // 需要被緩存
      }
    },
    {
      path: '/cart',
      component: Cart,
      name: 'Cart',
      meta: {
        title: '購物車',
        keepAlive: true // 需要被緩存
      }
    },
    {
      path: '/user',
      component: User,
      name: 'User',
      meta: {
        title: '我的',
        keepAlive: true // 需要被緩存
      }
    },
    {
      path: '/user-login',
      component: UserLogIn,
      name: 'UserLogIn',
      meta: {
        title: '登錄',
        keepAlive: false // 不需要被緩存
      }
    },
    {
      path: '/goods-detail',
      component: GoodsDetail,
      name: 'GoodsDetail',
      meta: {
        title: '商品詳情',
        keepAlive: true // 需要被緩存
      }
    }
  ],
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return {x: 0, y: 0}
    }
  }
})

// 全局路由守衛
router.beforeEach((to, from, next) => {
  const nextRoute = ['User', 'Cart', 'GoodsDetail'] // 需要登錄的頁面
  let isLogin = localTake('userMsg')  // 判斷是否登錄,本地存儲有用戶數據則視為已經登錄
  // 未登錄狀態;當路由到 nextRoute 指定頁時,跳轉至 UserLogIn
  if (nextRoute.indexOf(to.name) >= 0) { // 檢測是否登錄的頁面
    if (!isLogin) { // 如果未登錄(本地存儲無用戶數據),並且要跳到登錄頁面
      if (from.name === 'UserLogIn') {
        next('/')
        return
      }
    // 登錄后,跳到到當前頁面
      router.push({
        name: 'UserLogIn',
        params: {redirect: to.fullPath}  
      })
    }
  }
  // 已登錄狀態;當路由到 UserLogIn 時,跳轉至 Home
  if (to.name === 'UserLogIn') {
    if (isLogin) {
      next('/')
      return
    }
  }
  next() // 必須使用 next ,執行效果依賴 next 方法的調用參數
})

export default router

 

  方法二:通過定義to.meta.needLogin(needLogin 為自定義,路由元信息),判斷是否需要登錄

import Vue from 'vue'
import Router from 'vue-router'
import store from './../store'

import Home from 'components/home/home' // 主頁組件

// 其它組件...

import Cart from 'components/cart/cart' // 購物車組件
import User from 'components/user/user' // 用戶中心組件

// 其他組件...

import GoodsDetail from 'components/goods-detail/goods-detail' // 商品詳情組件

import { localTake } from 'common/js/localStore' // 本地存儲方法封裝

Vue.use(Router)
const router = new Router({
  routes: [
    {
      path: '/', // 默認地址
      redirect: '/home'
    },
    {
      path: '/home',
      component: Home,
      name: 'Home',
      meta: {
        title: '主頁',
        keepAlive: true // 需要被緩存
      }
    },
    {
      path: '/cart',
      component: Cart,
      name: 'Cart',
      meta: {
        title: '購物車',
        keepAlive: true, // 需要被緩存
        needLogin: true // 需要登錄 
      }
    },
    {
      path: '/user',
      component: User,
      name: 'User',
      meta: {
        title: '我的',
        keepAlive: true, // 需要被緩存
        needLogin: true // 需要登錄
      }
    },
    {
      path: '/user-login',
      component: UserLogIn,
      name: 'UserLogIn',
      meta: {
        title: '登錄',
        keepAlive: false // 不需要被緩存
      }
    },
    {
      path: '/goods-detail',
      component: GoodsDetail,
      name: 'GoodsDetail',
      meta: {
        title: '商品詳情',
        keepAlive: true, // 需要被緩存
        needLogin: true // 需要登錄
      }
    }
  ]
})
// 全局路由守衛
router.beforeEach((to, from, next) => {
  let isLogin = localTake('userMsg') 
  if (to.meta.needLogin) {  // 判斷該路由是否需要登錄權限
    if (isLogin) { // 判斷是否已經登錄
      next()
    }
    else {
      next({
        path: '/login',
        query: {redirect: to.fullPath}  // 將跳轉的路由path作為參數,登錄成功后跳轉到該路由
      })
    }
  }
  else {
    next()
  }
})


export default router

 

推薦使用判斷路由元信息的方法,代碼比較簡潔,能更好的維護與管理


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM