Search code examples
vue.jsnuxt.jsvuexstore

Best solution to update component content from another component in Nuxt


I'm new to Nuxt and Vue in general, so I'm not sure, what would be the best solution for this use case.

I want to change the text (like "Home", "Settings", "Favourites" etc.) inside my Header.vue (always fixed on top of the page) from another component. like Favourites.vue for example.

Sometimes I want to hide the header completely or hide the title and display buttons instead, so I need to pass more props than just the title.

I tried using different layouts, but that breaks the animation transitions (I haven't found the solution for this yet), but I think it's still better to have control from the page in what I'm passing into this component.


Should I pass props from Page.vue to parent and read it from there in Header.vue component?

Should I use Vuex to pass this through the store and update it when the route changes? Or is that too complex for this use case?

Maybe there's a simpler solution that I'm not aware of.


Folder structure:

/components
├──Header.vue
└──Nav.vue

/pages
├──Index.vue
├──Profile.vue
├──Settings.vue
└──Favourites.vue

Solution

  • Vuex is the answer here— don’t worry about the 'simple' use case. As soon as you notice you’re creating data that other components may rely on (is the header visible on this page? Is the text different? etc) it’s a good idea to move into Vuex and maintain a single source of truth.

    Your app may seem simple to begin with, but it’ll inevitably grow and at that point you’ll appreciate having a single source of truth vs. trying to pass things between components via props.

    Nuxt also makes implementing Vuex very straight forward. No doubt you’re capable of pulling up the docs!