开飞机的猪
开飞机的猪
  • 发布:2020-04-27 14:35
  • 更新:2020-04-27 14:35
  • 阅读:1661

vue路由钩子拦截器beforeEach和afterEach在APP上失效,小程序就没问题

分类:uni-app

import Vue from 'vue'
import Router from 'uni-simple-router'
import routes from './routers.js'
import store from '../store/index.js'
import { setToken, getToken } from '../util/'
Vue.use(Router)

const router = new Router({
routes:routes

})
const LOGIN_PAGE_NAME = 'login'
const HOME_PAGE_NAME = 'index'

router.beforeEach((to, from, next) => {
const token = getToken();

if (!token && to.name !== LOGIN_PAGE_NAME) {  
    if(to.name == HOME_PAGE_NAME || to.name == 'gywm'){  
        // 未登录且要跳转的页面是首页或关于我们  
        next() // 跳转  
    }  
    // 未登录且要跳转的页面不是登录页  
    next({  
      name: LOGIN_PAGE_NAME ,// 跳转到登录页  
      NAVTYPE: 'replaceAll'  
    })  
  } else if (!token && to.name === LOGIN_PAGE_NAME) {  
    // 未登陆且要跳转的页面是登录页  
    next() // 跳转  
  } else if (token && to.name === LOGIN_PAGE_NAME) {  
    // 已登录且要跳转的页面是登录页  
    next({  
      name: HOME_PAGE_NAME ,// 跳转到首页  
      NAVTYPE: 'replaceAll'  
    })  
  } else {  
    if (store.state.user.hasGetInfo) {  
      next()  
    } else {  
      store.dispatch('getLoginUserInfo').then(res => {  
        if(res.code == 10001){  
            setToken('')  
            next({  
              name: 'login',  
              NAVTYPE: 'replaceAll'  
            })  
        }else{  
            next()  
        }  
      }).catch(() => {  
        setToken('')  
        next({  
          name: 'login',  
          NAVTYPE: 'replaceAll'  
        })  
      })  
    }  
  }  

})

router.afterEach(to => {

})

export default router
代码哪里有问题吗

2020-04-27 14:35 负责人:无 分享
已邀请:

该问题目前已经被锁定, 无法添加新回复