fix(vue): mock nuxtApp.hooks & useRuntimeHook

This commit is contained in:
Benjamin Canac
2025-03-28 14:56:06 +01:00
parent 88f349d0d7
commit 23bfeb9370
3 changed files with 19 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
import { ref } from 'vue'
import { ref, onScopeDispose } from 'vue'
import type { Ref, Plugin as VuePlugin } from 'vue'
import { createHooks } from 'hookable'
import appConfig from '#build/app.config'
import type { NuxtApp } from '#app'
@@ -58,13 +59,25 @@ export const useState = <T>(key: string, init: () => T): Ref<T> => {
return value as Ref<T>
}
const hooks = createHooks()
export function useNuxtApp() {
return {
isHydrating: true,
payload: { serverRendered: false }
payload: { serverRendered: false },
hooks,
hook: hooks.hook
}
}
export function useRuntimeHook(name: string, fn: (...args: any[]) => void): void {
const nuxtApp = useNuxtApp()
const unregister = nuxtApp.hook(name, fn)
onScopeDispose(unregister)
}
export function defineNuxtPlugin(plugin: (nuxtApp: NuxtApp) => void) {
return {
install(app) {