zoukankan      html  css  js  c++  java
  • vue-router beforeEach死循环

    vue中页面跳墙处理

    页面跳墙中使用 vue-router中的 beforeEach的死循环问题

    • 问题展现
    import Router from 'vue-router'
    const router = new Router({
        {path: '/', component: index },
        {path: '/login', component: login},
        {path: '/error', component: error},
        {path: '*', component: error}
    })
    
    router.beforeEach((to, from, next) => {
        const isLogin = sessionStorage.getItem('loginData')
        if (isLogin) {
            next()
        } else {
            next('/error')
        } 
    })
    

    最近在使用时,一直陷入死循环,当时的想法是如何将路由提取出来,脱离beforeEach的控制,之后发现不可行。上面问题再现,会出现死循环,因为 /error 会在进入前 又要进入beforeEach中 ,这样就会一直循环下去
    所以就是想如何跳出这个循环即可

    router.beforeEach((to, from, next) => {
        const isLogin = sessionStorage.getItem('loginData')
        if (isLogin) {
            next()
        } else {
            //next('/error')
            if (to.path === '/error') { //这就是跳出循环的关键
               next()
            } else {
                next('/error')
            }
        } 
    })
    

    这样写,其实这个会执行两次,第二次进来是以/error的路由进来的

    总结

    • 看文档也需要 实践哈
  • 相关阅读:
    Nginx-php-mysql
    Smokeping配置调整
    Smokeping外置邮箱告警
    部署Smokeping
    Centos7部署cacti
    通过yum安装最新服务
    Cacti部署
    MySQL磁盘写入策略以及数据安全性的相关参数
    linux中一些特殊的中文文件不能删除问题
    Slave_SQL_Running:No的两种解决办法
  • 原文地址:https://www.cnblogs.com/sinosaurus/p/9016163.html
Copyright © 2011-2022 走看看