From a8013e1f0b235bd2b46c0d74190800c28efe945f Mon Sep 17 00:00:00 2001 From: Phan An Date: Sat, 8 Oct 2022 13:57:54 +0200 Subject: [PATCH] fix: route redirection not fully working (#1520) --- resources/assets/js/config/routes.ts | 12 +++++------- resources/assets/js/router.ts | 12 ++++++------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/resources/assets/js/config/routes.ts b/resources/assets/js/config/routes.ts index 4b059196..075408d3 100644 --- a/resources/assets/js/config/routes.ts +++ b/resources/assets/js/config/routes.ts @@ -2,13 +2,7 @@ import { eventBus } from '@/utils' import { Route } from '@/router' import { userStore } from '@/stores' -const queueRoute: Route = { - path: '/queue', - screen: 'Queue' -} - export const routes: Route[] = [ - queueRoute, { path: '/home', screen: 'Home' @@ -17,6 +11,10 @@ export const routes: Route[] = [ path: '/404', screen: '404' }, + { + path: '/queue', + screen: 'Queue' + }, { path: '/songs', screen: 'Songs' @@ -87,7 +85,7 @@ export const routes: Route[] = [ { path: '/song/(?[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})', screen: 'Queue', - redirect: () => queueRoute, + redirect: () => 'queue', onBeforeEnter: params => eventBus.emit('SONG_QUEUED_FROM_ROUTE', params.id) } ] diff --git a/resources/assets/js/router.ts b/resources/assets/js/router.ts index 387411fa..26b3c8a8 100644 --- a/resources/assets/js/router.ts +++ b/resources/assets/js/router.ts @@ -3,7 +3,7 @@ import { ref, Ref, watch } from 'vue' type RouteParams = Record type BeforeEnterHook = (params: RouteParams) => boolean | void type EnterHook = (params: RouteParams) => any -type RedirectHook = (params: RouteParams) => Route +type RedirectHook = (params: RouteParams) => Route | string export type Route = { path: string @@ -59,6 +59,11 @@ export default class Router { return this.triggerNotFound() } + if (route.redirect) { + const to = route.redirect(routeParams) + return typeof to === 'string' ? this.go(to) : this.activateRoute(to, routeParams) + } + return this.activateRoute(route, routeParams) } } @@ -78,11 +83,6 @@ export default class Router { this.$currentRoute.value = route this.$currentRoute.value.params = params - if (this.$currentRoute.value.redirect) { - const to = this.$currentRoute.value.redirect(params) - return await this.activateRoute(to, to.params) - } - if (this.$currentRoute.value.onEnter) { await this.$currentRoute.value.onEnter(params) }