vue-router配置

vue项目中vue-router的处理

import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/login'
import Error from '@/views/error'
import ModifyPassword from '@/views/modifypassword'
import Resetpsd from '@/views/resetpsd'
import MailConfirm from '@/views/mailconfirm'
import {getCookie} from '../utils/util'
// 或者你可以新建一个方法
Router.prototype.goBack = () => {
    merchantwallet.rountisBack = true
    window.history.go(-1)
}
Vue.use(Router)

const router = new Router({
    mode: 'history',
    base: '/',
    routes: [
        {
            path: '/error',
            name: '找不到该页面',
            component: Error
        },
        {
            path: '/login',
            name: '登录',
            component: Login
        },
        {
            path: '/modifypassword',
            name: '修改密码',
            component: ModifyPassword,
            meta:{ requiresAuth: true }  //需要鉴权
        },
        {
            path: '/resetpsd',
            name: '找回密码',
            component: Resetpsd
        },
        {
            path: '/mailconfirm',
            name: '邮件确认',
            component: MailConfirm
        },
        {
            path: '/...',
            name: '功能页',
            component: ...,
            meta:{ requiresAuth: true }  //需要鉴权
        }
    ]
})

/**
 *  路由拦截
 *  所有需要鉴权的页面,如果存储登录态的cookie不存在就跳登录页
 */
router.beforeEach((to,from,next)=>{
    if(to.matched.some(record=>record.meta.requiresAuth)){   //遍历 $route.matched 来检查路由记录中的 meta 字段
        if(getCookie('session')){
            next()          //进行路由管道中的下一个钩子
        }else{
            next({
                path: '/login',
                query: { redirect : to.fullPath }
            })
        }
    }else{
        next()
    }
})

export default router;

 

转载于:https://www.cnblogs.com/wxcbg/p/10950453.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值