博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vue登录注册及token验证
阅读量:4949 次
发布时间:2019-06-11

本文共 959 字,大约阅读时间需要 3 分钟。

// router.js

import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [

{
path: '/',
component: require('./views/Home'),
meta: {
requiresAuth: true
}
},
]

const router = new VueRouter({

routes: routes
})

router.beforeEach((to, from, next) => {

let token = window.localStorage.getItem('token')
if (to.matched.some(record => record.meta.requiresAuth) && (!token || token === null)) {
next({
path: '/login',
query: { redirect: to.fullPath }
})
} else {
next()
}
})

export default router

<script>

// App.vue
export default {
watch:{
'$route':function(to,from){
let token = window.localStorage.getItem('token');
         if (to.matched.some(record => record.meta.requiresAuth) && (!token || token === null)) {
           next({
           path: '/login',
           query: { redirect: to.fullPath }
           })
         } else {
       next()
         }
   }
  }
}
</script>

转载于:https://www.cnblogs.com/tis100204/p/10442727.html

你可能感兴趣的文章
2019牛客暑期多校训练营(第二场)- H Second Large Rectangle
查看>>
HDU3949 XOR
查看>>
2019牛客暑期多校训练营(第四场)- sequence
查看>>
2019 Multi-University Training Contest 2 - Longest Subarray
查看>>
2019 Multi-University Training Contest 3 - D Distribution of books
查看>>
2019 Multi-University Training Contest 4 - Minimal Power of Prime
查看>>
2019 Multi-University Training Contest 4 - K-th Closest Distance
查看>>
2019牛客暑期多校训练营(第六场)- Shorten IPv6 Address
查看>>
2019牛客暑期多校训练营(第五场)- generator 1
查看>>
2019 Multi-University Training Contest 6 - Snowy Smile
查看>>
2019牛客暑期多校训练营(第六场)- Upgrading Technology
查看>>
2019牛客暑期多校训练营(第七场)- String
查看>>
2019牛客暑期多校训练营(第七场)- Governing sand
查看>>
2019牛客暑期多校训练营(第八场)- Explorer
查看>>
2019牛客暑期多校训练营(第七场)- Find the median
查看>>
HDU1542 Atlantis
查看>>
POJ 2201 Cartesian Tree
查看>>
洛谷P3796 AC自动机
查看>>
BZOJ 1176 Mokia
查看>>
洛谷P2414 阿狸的打字机
查看>>