router.afterEach((to, from) => {
// 函式內寫程式碼
})
和beforeEach不同,afterEach不會在函式內return任何東西,也沒有next()
router.beforeEach((to, from) => {
if (to.name === 'Dashboard' && !isLoggedIn()) {
return { name: 'Login' }
}
})
router.afterEach((to, from) => {
console.log('afterEach 執行!')
})
假設有一段邏輯
「我要進入dashboard,但因無權限,被重新導向login頁」
它會這樣執行:
上述邏輯,beforeEach共執行2次,afterEach只會執行1次