mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
chore: improve internal type safety (#19599)
This commit is contained in:
parent
42986a4669
commit
e6bbf71fd5
@ -1,13 +1,17 @@
|
|||||||
import { getCurrentInstance, inject, onUnmounted } from 'vue'
|
import { getCurrentInstance, inject, onUnmounted } from 'vue'
|
||||||
|
import type { Ref } from 'vue'
|
||||||
import type { Router, RouteLocationNormalizedLoaded, NavigationGuard, RouteLocationNormalized, RouteLocationRaw, NavigationFailure, RouteLocationPathRaw } from 'vue-router'
|
import type { Router, RouteLocationNormalizedLoaded, NavigationGuard, RouteLocationNormalized, RouteLocationRaw, NavigationFailure, RouteLocationPathRaw } from 'vue-router'
|
||||||
import { sendRedirect } from 'h3'
|
import { sendRedirect } from 'h3'
|
||||||
import { hasProtocol, joinURL, parseURL } from 'ufo'
|
import { hasProtocol, joinURL, parseURL } from 'ufo'
|
||||||
|
|
||||||
import { useNuxtApp, useRuntimeConfig } from '../nuxt'
|
import { useNuxtApp, useRuntimeConfig } from '../nuxt'
|
||||||
import type { NuxtError } from './error'
|
import type { NuxtError } from './error'
|
||||||
import { createError } from './error'
|
import { createError } from './error'
|
||||||
import { useState } from './state'
|
import { useState } from './state'
|
||||||
import { setResponseStatus } from './ssr'
|
import { setResponseStatus } from './ssr'
|
||||||
|
|
||||||
|
import type { PageMeta } from '#app'
|
||||||
|
|
||||||
export const useRouter = () => {
|
export const useRouter = () => {
|
||||||
return useNuxtApp()?.$router as Router
|
return useNuxtApp()?.$router as Router
|
||||||
}
|
}
|
||||||
@ -148,11 +152,11 @@ export const setPageLayout = (layout: string) => {
|
|||||||
const inMiddleware = isProcessingMiddleware()
|
const inMiddleware = isProcessingMiddleware()
|
||||||
if (inMiddleware || process.server || nuxtApp.isHydrating) {
|
if (inMiddleware || process.server || nuxtApp.isHydrating) {
|
||||||
const unsubscribe = useRouter().beforeResolve((to) => {
|
const unsubscribe = useRouter().beforeResolve((to) => {
|
||||||
to.meta.layout = layout
|
to.meta.layout = layout as Exclude<PageMeta['layout'], Ref | false>
|
||||||
unsubscribe()
|
unsubscribe()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if (!inMiddleware) {
|
if (!inMiddleware) {
|
||||||
useRoute().meta.layout = layout
|
useRoute().meta.layout = layout as Exclude<PageMeta['layout'], Ref | false>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -303,10 +303,10 @@ export function isNuxtPlugin (plugin: unknown) {
|
|||||||
export function callWithNuxt<T extends (...args: any[]) => any> (nuxt: NuxtApp | _NuxtApp, setup: T, args?: Parameters<T>) {
|
export function callWithNuxt<T extends (...args: any[]) => any> (nuxt: NuxtApp | _NuxtApp, setup: T, args?: Parameters<T>) {
|
||||||
const fn: () => ReturnType<T> = () => args ? setup(...args as Parameters<T>) : setup()
|
const fn: () => ReturnType<T> = () => args ? setup(...args as Parameters<T>) : setup()
|
||||||
if (process.server) {
|
if (process.server) {
|
||||||
return nuxtAppCtx.callAsync(nuxt, fn)
|
return nuxtAppCtx.callAsync(nuxt as NuxtApp, fn)
|
||||||
} else {
|
} else {
|
||||||
// In client side we could assume nuxt app is singleton
|
// In client side we could assume nuxt app is singleton
|
||||||
nuxtAppCtx.set(nuxt)
|
nuxtAppCtx.set(nuxt as NuxtApp)
|
||||||
return fn()
|
return fn()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { computed, isReadonly, reactive, shallowRef } from 'vue'
|
import { computed, isReadonly, reactive, shallowRef } from 'vue'
|
||||||
|
import type { Ref } from 'vue'
|
||||||
import type {
|
import type {
|
||||||
NavigationGuard,
|
NavigationGuard,
|
||||||
RouteLocation
|
RouteLocation
|
||||||
@ -11,6 +12,8 @@ import {
|
|||||||
} from 'vue-router'
|
} from 'vue-router'
|
||||||
import { createError } from 'h3'
|
import { createError } from 'h3'
|
||||||
import { withoutBase, isEqual } from 'ufo'
|
import { withoutBase, isEqual } from 'ufo'
|
||||||
|
|
||||||
|
import type { PageMeta } from '#app'
|
||||||
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig } from '#app/nuxt'
|
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig } from '#app/nuxt'
|
||||||
import { showError, clearError, useError } from '#app/composables/error'
|
import { showError, clearError, useError } from '#app/composables/error'
|
||||||
import { useRequestEvent } from '#app/composables/ssr'
|
import { useRequestEvent } from '#app/composables/ssr'
|
||||||
@ -118,7 +121,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|||||||
router.beforeEach(async (to, from) => {
|
router.beforeEach(async (to, from) => {
|
||||||
to.meta = reactive(to.meta)
|
to.meta = reactive(to.meta)
|
||||||
if (nuxtApp.isHydrating && initialLayout.value && !isReadonly(to.meta.layout)) {
|
if (nuxtApp.isHydrating && initialLayout.value && !isReadonly(to.meta.layout)) {
|
||||||
to.meta.layout = initialLayout.value
|
to.meta.layout = initialLayout.value as Exclude<PageMeta['layout'], Ref | false>
|
||||||
}
|
}
|
||||||
nuxtApp._processingMiddleware = true
|
nuxtApp._processingMiddleware = true
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user