溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Vuerouter中beforeEach與afterEach函數(shù)有什么區(qū)別

發(fā)布時(shí)間:2021-03-17 14:44:43 來(lái)源:億速云 閱讀:308 作者:Leah 欄目:web開(kāi)發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)Vuerouter中beforeEach與afterEach函數(shù)有什么區(qū)別,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

定義:路由鉤子主要是給使用者在路由發(fā)生變化時(shí)進(jìn)行一些特殊的處理而定義的函數(shù)。

總體來(lái)講vue里面提供了三大類鉤子,兩種函數(shù)
1、全局鉤子
2、某個(gè)路由的鉤子
3、組件內(nèi)鉤子

兩種函數(shù):

1、Vue.beforeEach(function(to,form,next){}) /*在跳轉(zhuǎn)之前執(zhí)行*/

2.Vue.afterEach(function(to,form))/*在跳轉(zhuǎn)之后判斷*/

全局鉤子函數(shù)

顧名思義,它是對(duì)全局有效的一個(gè)函數(shù)

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

  let token = router.app.$storage.fetch("token");

  let needAuth = to.matched.some(item => item.meta.login);

  if(!token && needAuth) return next({path: "/login"});

  next();

});

beforeEach函數(shù)有三個(gè)參數(shù):

  • to:router即將進(jìn)入的路由對(duì)象

  • from:當(dāng)前導(dǎo)航即將離開(kāi)的路由

  • next:Function,進(jìn)行管道中的一個(gè)鉤子,如果執(zhí)行完了,則導(dǎo)航的狀態(tài)就是 confirmed (確認(rèn)的);否則為false,終止導(dǎo)航。

afterEach函數(shù)不用傳next()函數(shù)

某個(gè)路由的鉤子函數(shù)

顧名思義,它是寫在某個(gè)路由里頭的函數(shù),本質(zhì)上跟組件內(nèi)函數(shù)沒(méi)有區(qū)別。

const router = new VueRouter({
 routes: [
  {
   path: '/login',
   component: Login,
   beforeEnter: (to, from, next) => {
    // ...
   },
   beforeLeave: (to, from, next) => {
    // ...
   }
  }
 ]
})

路由組件的鉤子

注意:這里說(shuō)的是路由組件!

路由組件 屬于 組件,但組件 不等同于 路由組件!所謂的路由組件:直接定義在router中component處的組件。如:

var routes = [
  {
  path:'/home',
  component:home,
  name:"home"
  }
]

在子組件中調(diào)用路由的鉤子函數(shù)時(shí)無(wú)效的。

在官方文檔上是這樣定義的:

可以在路由組件內(nèi)直接定義以下路由導(dǎo)航鉤子

  • beforeRouteEnter

  • beforeRouteUpdate (2.2 新增)

  • beforeRouteLeave

這里簡(jiǎn)單說(shuō)下鉤子函數(shù)的用法:它是和data,methods平級(jí)的。

beforeRouteLeave(to, from, next) {
  next()
},
beforeRouteEnter(to, from, next) {
  next()
},
beforeRouteUpdate(to, from, next) {
  next()
},
data:{},
method: {}

PS:在使用vue-router beforeEach鉤子時(shí),你也許會(huì)遇到如下問(wèn)題:

源碼:

router.beforeEach((to, from, next) => {
//判斷登錄狀態(tài)簡(jiǎn)單實(shí)例
var userInfo = window.localStorage.getItem('token');
if (userInfo) {
next();
} else {
next('/login');
}
})

然后你會(huì)發(fā)現(xiàn)出現(xiàn)如下錯(cuò)誤:出現(xiàn)dead loop錯(cuò)誤

Vuerouter中beforeEach與afterEach函數(shù)有什么區(qū)別

解決方案:

router.beforeEach((to, from, next) => {
var userInfo = window.localStorage.getItem('token');//獲取瀏覽器緩存的用戶信息
if(userInfo){ //如果有就直接到首頁(yè)咯
next();
} else {
if(to.path=='/login'){ //如果是登錄頁(yè)面路徑,就直接next()
next();
} else { //不然就跳轉(zhuǎn)到登錄;
next('/login');
}

}
})

關(guān)于Vuerouter中beforeEach與afterEach函數(shù)有什么區(qū)別就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI