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?
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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With