Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass props to <nuxt /> component pages

Tags:

nuxt.js

Has anyone been able to pass props to pages in Nuxt.js?

Typical Vue.js props can be passed as so:

// parent-component.vue


<child-component :basket-count="items.length"/>

<script>
import ChildComponent from './child-component'
export default {
  data () {
    items: [{}, {}]
  },
  components: {
    childComponent: ChildComponent
  }
}
</script>


// child-component.vue


<p>You have {{ basketCount }} items in your basket</p>

<script>
export default {
  props: [
    'basket-count'
  ]
}
</script>

But when using the <nuxt /> tag in a layout default.vue, props do not get passed to the children pages as they should.

Discussion has taken place already here, but the ticket appears to have no conclusion. I cannot yet troubleshoot a working means of achieving this.

Will post back, but curious to know thoughts from Nuxt.js devs on this?

like image 988
CodeUK Avatar asked Jun 11 '18 14:06

CodeUK


People also ask

Can I use vue3 with NUXT?

Vue 3 and Vite SupportWith Nuxt 3 being written in Vue 3, you get access to features like the Composition API, better module imports, and overall improved app performance. Nuxt 3 also comes with Vite support, which is backwards compatible with Nuxt 2.

How do I use props to pass data to child components in Vue?

The way it works is that you define your data on the parent component and give it a value, then you go to the child component that needs that data and pass the value to a prop attribute so the data becomes a property in the child component. You can use the root component (App.

How do I add components to NUXT?

To dynamically import a component, also known as lazy loading a component, all you need to do is add the Lazy prefix in your templates. Using the lazy prefix you can also dynamically import a component when an event is triggered.

How do I transfer data to Vue props?

To specify the type of prop you want to use in Vue, you will use an object instead of an array. You'll use the name of the property as the key of each property, and the type as the value. If the type of the data passed does not match the prop type, Vue sends an alert (in development mode) in the console with a warning.


1 Answers

Use NuxtChild

Just Use NuxtChild instead of Nuxt and you can directly pass everything to the pages/compnents that are loaded. Docs

so in layout do something like this


  <div id="app">
    <Header />
    <SocialMedia />
    <main>
      <NuxtChild :myPropNameHere="prop data here" />
    </main>
    <Aside />
  </div>


like image 141
Yugank Singh Avatar answered Jan 03 '23 15:01

Yugank Singh