mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-29 09:02:03 +00:00
feat(nuxt): start loading indicator state with middleware (#21003)
This commit is contained in:
parent
bcb3d7337b
commit
7e74e7c2fc
@ -1,5 +1,9 @@
|
|||||||
import { computed, defineComponent, h, onBeforeUnmount, ref } from 'vue'
|
import { computed, defineComponent, h, onBeforeUnmount, ref } from 'vue'
|
||||||
import { useNuxtApp } from '#app/nuxt'
|
import { useNuxtApp } from '#app/nuxt'
|
||||||
|
import { useRouter } from '#app/composables/router'
|
||||||
|
|
||||||
|
// @ts-expect-error virtual file
|
||||||
|
import { globalMiddleware } from '#build/middleware'
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
name: 'NuxtLoadingIndicator',
|
name: 'NuxtLoadingIndicator',
|
||||||
@ -30,10 +34,20 @@ export default defineComponent({
|
|||||||
// Hook to app lifecycle
|
// Hook to app lifecycle
|
||||||
// TODO: Use unified loading API
|
// TODO: Use unified loading API
|
||||||
const nuxtApp = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
nuxtApp.hook('page:start', indicator.start)
|
const router = useRouter()
|
||||||
|
|
||||||
|
globalMiddleware.unshift(indicator.start)
|
||||||
|
router.beforeResolve((to, from) => {
|
||||||
|
if (to === from) {
|
||||||
|
indicator.finish()
|
||||||
|
}
|
||||||
|
})
|
||||||
nuxtApp.hook('page:finish', indicator.finish)
|
nuxtApp.hook('page:finish', indicator.finish)
|
||||||
nuxtApp.hook('vue:error', indicator.finish)
|
nuxtApp.hook('vue:error', indicator.finish)
|
||||||
onBeforeUnmount(indicator.clear)
|
onBeforeUnmount(() => {
|
||||||
|
globalMiddleware.splice(globalMiddleware.indexOf(indicator.start, 1))
|
||||||
|
indicator.clear()
|
||||||
|
})
|
||||||
|
|
||||||
return () => h('div', {
|
return () => h('div', {
|
||||||
class: 'nuxt-loading-indicator',
|
class: 'nuxt-loading-indicator',
|
||||||
|
Loading…
Reference in New Issue
Block a user