BeforeEach doesn't work, I am doing something wrong

Hi everyone,

Am using Vuejs for not too long. I have a little problem with the ‘beforeEach’ function for the router.

Here is my code:
19caac8269af553181f9aca0b624f0cb

Below you see the ‘beforeEach’ function just very simple console.log a ‘hello’ message. But when ever i go to a route it doesn’t console.log anything.

Am I placing it in a wrong section?

Ty for reading this.

Did you try ? (https://router.vuejs.org/en/advanced/navigation-guards.html)

const router = new VueRouter({ ... })

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

I did, but it dident see my routes at all. the console log did tho

Did you export default router and import it to Vue instance?

router/index.js

const router = new VueRouter({ ... })

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

export default router

main.js

import router from './router'

new Vue({
  router,
  ...
})

Ah i know what I did wrong, i forgot to call the ‘next()’ function. Ty for helping me!

1 Like

Thanks you too.
Correct way is:

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