nuxt: Mounted hook not being called in components on production?

Hello!

I have a component that is included on a page in Nuxt.

This component has a mounted hook:

<script>
export default {
	name: 'MyComponent',
	mounted() { alert('oh hai!') }
}
</script>

This works fine in development. However when I export the site full static (nuxt generate) the code in the mounted hook appears not to run – the alert never appears when the page is loaded.

I feel like I must be missing something obvious, but I can’t figure it out. Do the created/mounted lifecycle hooks get called on the components if they are used on full static sites? If not, what is the recommended way to initialize the code that is used to control a component?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 32 (7 by maintainers)

Commits related to this issue

Most upvoted comments

@rabbitfufu find out I have page/index.vue in my project (SSG) when I server folder dist, and open ‘http://127.0.0.1:5500/’, mounted will trigger in pages and components used in pages, but mounted will not trigger when open ‘http://127.0.0.1:5500/index.html

@rabbitfufu find out I have page/index.vue in my project (SSG) when I server folder dist, and open ‘http://127.0.0.1:5500/’, mounted will trigger in pages and components used in pages, but mounted will not trigger when open ‘http://127.0.0.1:5500/index.html

Me too! Is there any idea to deal with this?

node: 14.17.0 nuxt: 2.15.8

nginx 301