vue:路由守衛

1:首先在路由對象中的某一個具體的路由對象加這樣一個屬性vue

meta: {  requireAuth:true  }vuex

2:而後在main.js中添加這段代碼ui

router.beforeEach((to, from, next) => {
if (to.meta.requireAuth) { // 判斷該路由是否須要登陸權限
if (localStorage.getItem('a')) { // 經過vuex state獲取當前的token是否存在,經過一個變量(vuex中或localstorage中),若是爲真,那麼跳轉localstorage

next();
}else {
alert('請輸入帳號和密碼')
next({
path: '/',//若是爲假,則重定向到這個路由路勁
query: {redirect: to.fullPath} // 將跳轉的路由path做爲參數,登陸成功後跳轉到該路由
})
}
}else {
next()
}
})router

 

結束!!!這樣就能在跳轉以前攔截並作一些操做。對象

相關文章
相關標籤/搜索