Vue: Can\’t access Pinia Store in beforeEnter vue-router

Vue provides support for some functions in which we need store(outside of the components).

To fix this problem I just called the useStore() function inside the function provided by Vue(beforeEach) and it worked.

import { useAuthStore } from \"@/stores/auth\";
.
.
.
.
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});
router.beforeEach(async (to, from) => {
  const authStore = useAuthStore();
  // use authStore Here
});

You may also like...