zoukankan      html  css  js  c++  java
  • 阻止路由跳转,以及路由重定向

    在main.js 文件中

     1 import router from './router'
     2 router.beforeEach((to, from, next) => {
     3   // 阻止路由跳转
     4   if (满足阻止条件时) {
     5       next(false)
     6       return false
     7   }
     8   // 如果传入的url中有token,则不必跳转登录页
     9   if (window.location.href.indexOf('token') > -1 && window.location.href.indexOf('?') > -1 && (router.mode === 'hash' || router.mode === 'history')) {
    10       let params = ''
    11       if (router.mode === 'hash') {
    12         params = window.location.href.split('#')[1]
    13         params = params.split('?')[1]
    14       } else {
    15         params = window.location.href.split('?')[1]
    16       }
    17       let paramsArray = params.split('&')
    18       let query = Object.create(null)
    19       paramsArray.forEach(elem => {
    20         let e = elem.split('=')
    21         query[e[0]] = e[1]
    22       })
    23       store.commit('userInfo', query)
    24       $axios.defaults.headers.Authorization = 'Bearer ' + query.token // 防止登录成功token还是undefined
    25       next()
    26   } else if (to.path === '/login' || store.state.userInfo.token) {
    27     next()
    28   } else {
    29     next('/login')
    30   }
    31   return false
    32 })
  • 相关阅读:
    map & reduce
    Generator
    切片
    函数参数
    Dict & Set
    list,tuple
    selenium鼠标和键盘操作
    selenium元素定位以及点击事件
    css定位
    xpath
  • 原文地址:https://www.cnblogs.com/haishen/p/10973235.html
Copyright © 2011-2022 走看看