记录一下全局路由守卫的使用;

  方法一:定义一个数组用于检测与管理需要登录的页面,全局路由守卫配合本地存储判断是否跳转

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

推荐使用判断路由元信息的方法,代码比较简洁,能更好的维护与管理

Vue router 全局路由守卫的更多相关文章

  1. vue全局路由守卫beforeEach+token验证+node

    在后端安装jsonwebtoken         npm i jsonwebtoken --save 在 login.js文件中引入      // 引入jwtconst jwt = require ...

  2. Vue router 一个路由对应多个视图

    使用命名路由 https://jsfiddle.net/posva/6du90epg/ <script src="https://unpkg.com/vue/dist/vue.js&q ...

  3. Vue 中的Vue Router一级路由,二级路由,三级路由以及跳转

    今天编写了一下Vue中的路由 先用命令行新建一个空的项目,并且我知道要用路由,就下载了路由的相关依赖 vue init webpack demo5 完毕之后进入所在的项目 cd demo5 之后用vs ...

  4. vue全局路由守卫beforeEach

    在main.js里使用方法 router.beforeEach((to,from,next)=>{}) to,是将要跳转的路由, from,是离开的路由 next是个方法,判断to.path 或 ...

  5. 5分钟学会vue中的路由守卫(导航守卫)

    在项目开发中每一次路由的切换或者页面的刷新都需要判断用户是否已经登录,前端可以判断,后端也会进行判断的,我们前端最好也进行判断. vue-router提供了导航钩子:全局前置导航钩子 beforeEa ...

  6. vue router动态路由

    <div id="#app"> <router-link to="/user/header">路由1</router-link&g ...

  7. vue router引入路由与路由配置容易犯错的地方与常见的报错与处理报错

    首先npm安装vue-router插件,就不说了其次: 先看下我本地的目录结构吧 第一步:在src目录下新建一个专门存放router的index.js文件里面的内容为: import Vue from ...

  8. Vue 2.0 路由全局守卫

    vue2.0 实现导航守卫(路由守卫) 路由跳转前做一些验证,比如登录验证,是网站中的普遍需求. 对此,vue-route 提供的 beforeRouteUpdate 可以方便地实现导航守卫(navi ...

  9. vue路由守卫应用,监听是否登录

    路由跳转前做一些验证,比如登录验证,是网站中的普遍需求. 对此,vue-route 提供的 beforeRouteUpdate 可以方便地实现导航守卫(navigation-guards). 导航守卫 ...

随机推荐

  1. Using Service Workers

    [Using Service Workers] 1.This is an experimental technology Because this technology's specification ...

  2. 【Spider】使用CrawlSpider进行爬虫时,无法爬取数据,运行后很快结束,但没有报错

    在学习<python爬虫开发与项目实践>的时候有一个关于CrawlSpider的例子,当我在运行时发现,没有爬取到任何数据,以下是我敲的源代码:import scrapyfrom UseS ...

  3. CRM销售管理功能

    联系项目project:-------是一个大的项目,比如通知开会之类 每个坐席需要分配自己的联系任务,每个联系任务,有自己的完成未完成状态.同时关联着通话记录等 销售计划----销售项目 销售流程: ...

  4. Floyd算法简介

    参考:https://blog.csdn.net/qq_35644234/article/details/60875818 一.Floyd算法的介绍    1.算法的特点:    弗洛伊德算法是解决任 ...

  5. tf.trainable_variables()

    https://blog.csdn.net/shwan_ma/article/details/78879620 一般来说,打印tensorflow变量的函数有两个:tf.trainable_varia ...

  6. Evaluate Reverse Polish Notation (STRING-TYPE CONVERTION)

    Question Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators a ...

  7. MySQL基本SQL语句之数据插入、删除数据和更新数据

    一.INSERT插入数据: 方法一:批量插入 基本语法: INSERT INTO tb_name (col1, col2, ...) VALUES (val1, val2, ...)[,(val1, ...

  8. vue初学:基础概念

    一.vue使用步骤: 1.引包vue.js 2.html中写要操作的DOM节点 3.创建vue对象:new Vue({options}); 4.配置options:el:(要操作的对象,用选择器,同j ...

  9. Python: Tkinter、ttk编程之计算器

    起源: 研究Python UI编程,我偏喜欢其原生组件,于是学习Tkinter.ttk组件用法.找一计算器开源代码,略加修整,以为备忘.其界面如图所示: 1.源代码(Python 2.7): # en ...

  10. 【Linux 线程】线程同步《二》

    1.读写锁 与互斥量类似,但读写锁允许更高的并行性.其特性为:写独占,读共享. 读写锁状态: 一把读写锁具备三种状态: (1)读模式下加锁状态 (读锁) (2)写模式下加锁状态 (写锁) (3)不加锁 ...