Preserve query params on route change

This commit is contained in:
Suven-p 2024-10-27 19:45:43 +05:45
parent c6a9a78175
commit b68cdf70d6

View file

@ -192,8 +192,24 @@ const routes = [
},
];
export const router = createRouter({
const router = createRouter({
linkActiveClass: "active",
history: createWebHistory(),
routes,
});
router.beforeEach((to, from) => {
// If the path is same, either the query or has has changed so avoid changing query params
// Without this check, modifying any query params will be blocked
// Check if redirectedFrom is defined to check if this function has already been run
// Without this check, the router will be stuck in an infinite loop
if (to.path !== from.path && !to.redirectedFrom) {
return {
path: to.path,
query: { ...to.query, ...from.query },
params: to.params,
}
}
});
export { router };