From 6b30a229bd03d08ef8528a9e3c6fdafce794214a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20P=C4=99dzich?= Date: Thu, 29 Jun 2023 19:48:00 +0200 Subject: [PATCH] Add scrollBehaviour function to scroll to fragment --- src/router/index.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/router/index.ts b/src/router/index.ts index ee4b685..cb173f4 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,4 +1,11 @@ -import { createRouter, createWebHistory } from 'vue-router'; +/* eslint-disable @typescript-eslint/no-unused-vars */ + +import { + NavigationGuardNext, + RouteLocationNormalized, + createRouter, + createWebHistory +} from 'vue-router'; import Home from '@/views/Home.vue'; import { useVote } from '@/composables/useVote'; @@ -20,12 +27,15 @@ const routes = [ path: '/ranking', name: 'Ranking', component: () => import('../views/Ranking.vue'), - // eslint-disable-next-line @typescript-eslint/no-unused-vars - beforeEnter: (_to: unknown, _from: unknown, next: () => void) => { + beforeEnter: ( + _to: RouteLocationNormalized, + _from: RouteLocationNormalized, + next: NavigationGuardNext + ) => { if (userSubmittedAllVotes.value) { return next(); } else { - return false; + return next('/'); } } } @@ -33,7 +43,14 @@ const routes = [ const router = createRouter({ history: createWebHistory(process.env.BASE_URL), - routes + routes, + scrollBehavior(to, _from, _savedPosition) { + if (to.hash) { + return { + el: to.hash + }; + } + } }); export default router;