I’m building a nuxt module, and want to set loadingTemplate within this module.
What I tried so far is modifying devServer
inside module like below (But no luck)
import { loadingTemplate } from './loadingTemplate'
export default defineNuxtModule<ModuleOptions>({
...
async setup (options, nuxt) {
nuxt.options.devServer = {
...nuxt.options.devServer,
loadingTemplate
}
}
})
I’ve also tried doing it via hooks
:
import { loadingTemplate } from './loadingTemplate'
export default defineNuxtModule<ModuleOptions>({
...
async setup (options, nuxt) {
nuxt.hook('ready', (nuxtApp) => {
nuxtApp.devServer = {
...nuxt.devServer,
loadingTemplate
}
})
}
})
I’ve tried different hooks: ready
, listen
Can someone point me in correct direction on how to use custom loadingTemplate
that will be set from my nuxt module?
2
Answers
Try using Nuxt hooks. If that doesn’t work, you can check out this link
You’re on the right track! Here’s how to correctly set the
loadingTemplate
within your Nuxt module:The Key:
buildModules
The
loadingTemplate
is set during the build process, so you need to modify the Nuxt configuration within thebuildModules
array of your module.Revised Module Structure:
Explanation:
build:before
Hook: Thebuild:before
hook runs before the Nuxt build process starts. This is the ideal place to modify build configuration.buildModules
Array: ThebuildModules
array holds configuration for additional build-time modules. We push a new module object that defines aname
(your module’s name) andloadingTemplate
to use.loadingTemplate
: This should contain your custom loading template content (a string) or a function that returns a string.Example
loadingTemplate
:Make sure you register your custom module in your
nuxt.config.js
file:Nuxt 3: The
loadingTemplate
is set in theapp.template
field for Nuxt 3.