2022-01-26 11:56:24 +00:00
|
|
|
import { KeepAliveProps, TransitionProps, UnwrapRef } from 'vue'
|
2022-01-25 12:29:11 +00:00
|
|
|
import type { Router, RouteLocationNormalizedLoaded, NavigationGuard, RouteLocationNormalized, RouteLocationRaw } from 'vue-router'
|
2021-12-17 09:15:03 +00:00
|
|
|
import { useNuxtApp } from '#app'
|
|
|
|
|
|
|
|
export const useRouter = () => {
|
|
|
|
return useNuxtApp().$router as Router
|
|
|
|
}
|
|
|
|
|
2021-12-20 10:37:00 +00:00
|
|
|
export const useRoute = () => {
|
|
|
|
return useNuxtApp()._route as RouteLocationNormalizedLoaded
|
2021-12-17 09:15:03 +00:00
|
|
|
}
|
2022-01-17 18:27:23 +00:00
|
|
|
|
|
|
|
export interface PageMeta {
|
|
|
|
[key: string]: any
|
2022-02-07 10:17:28 +00:00
|
|
|
pageTransition?: boolean | TransitionProps
|
|
|
|
layoutTransition?: boolean | TransitionProps
|
2022-01-24 10:35:28 +00:00
|
|
|
key?: string | ((route: RouteLocationNormalizedLoaded) => string)
|
2022-02-07 10:17:28 +00:00
|
|
|
keepalive?: boolean | KeepAliveProps
|
2022-01-17 18:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
declare module 'vue-router' {
|
2022-01-26 11:56:24 +00:00
|
|
|
interface RouteMeta extends UnwrapRef<PageMeta> {}
|
2022-01-17 18:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const warnRuntimeUsage = (method: string) =>
|
|
|
|
console.warn(
|
|
|
|
`${method}() is a compiler-hint helper that is only usable inside ` +
|
|
|
|
'<script setup> of a single file component. Its arguments should be ' +
|
|
|
|
'compiled away and passing it at runtime has no effect.'
|
|
|
|
)
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
export const definePageMeta = (meta: PageMeta): void => {
|
|
|
|
if (process.dev) {
|
|
|
|
warnRuntimeUsage('definePageMeta')
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 12:29:11 +00:00
|
|
|
|
|
|
|
export interface RouteMiddleware {
|
|
|
|
(to: RouteLocationNormalized, from: RouteLocationNormalized): ReturnType<NavigationGuard>
|
|
|
|
}
|
|
|
|
|
|
|
|
export const defineNuxtRouteMiddleware = (middleware: RouteMiddleware) => middleware
|
|
|
|
|
|
|
|
export interface AddRouteMiddlewareOptions {
|
|
|
|
global?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
export const addRouteMiddleware = (name: string, middleware: RouteMiddleware, options: AddRouteMiddlewareOptions = {}) => {
|
|
|
|
const nuxtApp = useNuxtApp()
|
|
|
|
if (options.global) {
|
|
|
|
nuxtApp._middleware.global.push(middleware)
|
|
|
|
} else {
|
|
|
|
nuxtApp._middleware.named[name] = middleware
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const isProcessingMiddleware = () => {
|
|
|
|
try {
|
|
|
|
if (useNuxtApp()._processingMiddleware) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} catch {}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
export const navigateTo = (to: RouteLocationRaw) => {
|
|
|
|
if (isProcessingMiddleware()) {
|
|
|
|
return to
|
|
|
|
}
|
|
|
|
const router: Router = process.server ? useRouter() : (window as any).$nuxt.router
|
|
|
|
return router.push(to)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** This will abort navigation within a Nuxt route middleware handler. */
|
|
|
|
export const abortNavigation = (err?: Error | string) => {
|
|
|
|
if (process.dev && !isProcessingMiddleware()) {
|
|
|
|
throw new Error('abortNavigation() is only usable inside a route middleware handler.')
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
throw err instanceof Error ? err : new Error(err)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|