designtopack/src/router/router.js

32 lines
765 B
JavaScript
Raw Normal View History

2024-09-17 17:03:13 +02:00
import { createWebHistory, createRouter } from "vue-router";
2024-09-17 18:11:42 +02:00
import routes from "./routes";
2024-09-17 17:03:13 +02:00
import { useApiStore } from "../stores/api";
import { usePageStore } from "../stores/page";
2024-10-28 15:33:52 +01:00
import { useUserStore } from "../stores/user";
2024-09-17 17:03:13 +02:00
import { getActivePinia } from "pinia";
const router = createRouter({
history: createWebHistory(),
routes,
});
router.beforeEach(async (to, from, next) => {
const pinia = getActivePinia();
const api = useApiStore(pinia);
2024-10-28 15:33:52 +01:00
const pageStore = usePageStore(pinia);
const userStore = useUserStore(pinia);
2024-09-17 17:03:13 +02:00
try {
2024-10-28 15:33:52 +01:00
const res = await api.fetchData(to.path);
2024-09-17 17:03:13 +02:00
2024-10-28 15:33:52 +01:00
pageStore.page = res.page;
userStore.user = res.user;
2024-09-17 17:03:13 +02:00
next();
} catch (error) {
console.error(error);
next(false);
}
});
export { router };