Answer the question
In order to leave comments, you need to log in
How to pass parameters from page to layout nuxt js?
layouts/default.vue
<template>
<div class="page__container">
<app-header/>
<main class="page__content">
<Nuxt/>
</main>
<app-footer />
</div>
</template>
<template>
<div class="page__container">
<app-header :header-data="headerData"/>
<main class="page__content">
<Nuxt/>
</main>
<app-footer />
</div>
</template>
<script>
export default {
name: 'default-layout',
components: {AppHeader, AppFooter},
computed: {
headerData() {
return this.$route.matched.map(r => {
return r.components.default.options.headerData
})[0]
}
}
}
</script>
export default {
headerData: {
title: "Заголовок страницы",
subtitle: "Подзаголовок страницы"
}
}
<template>
<header>
<h1>{{headerData.title}}</h1>
<span>{{headerData.subtitle}}</span>
</header>
</template>
<script>
export default {
name: "app-header",
props: {
headerData: {
type: Object,
required: true
}
},
}
</script>
Answer the question
In order to leave comments, you need to log in
In case someone stumbles upon this old question:
If you need to pass something from the page to some other component in the layout, then it is more correct (in my opinion) in this case to use vuex and change the variable in the store if necessary. Inside the layout, you need to pull up this variable and pass it to the props of the required component.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question