fix: route redirection not fully working (#1520)

This commit is contained in:
Phan An 2022-10-08 13:57:54 +02:00 committed by GitHub
parent d4f71b46b7
commit a8013e1f0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 13 deletions

View file

@ -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/(?<id>[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)
}
]

View file

@ -3,7 +3,7 @@ import { ref, Ref, watch } from 'vue'
type RouteParams = Record<string, string>
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)
}