Passing and Accessing Props in Nested Layouts with Nuxt 3

In our previous blog post, "Creating Nested Layouts in Nuxt 3," we explored how to nest layouts within pages to create a modular and maintainable structure for your Nuxt 3 application. Building upon that foundation, we will now delve into the exciting topic of passing and accessing props in nested layouts.

The Power of Props

Props are a fundamental concept in Vue.js, and they are incredibly useful when it comes to passing data between components. In the context of Nuxt 3 layouts, props allow you to customize and inject data into your layouts on a per-page basis. This flexibility is particularly handy when you want to make your layouts dynamic and adaptable.

Passing Props from a Page to a Layout

Let's extend our previous example of nested layouts in Nuxt 3, where we have a "foo" page and a nested "bar" layout. Now, we want to pass a prop from the "foo" page to the "bar" layout.

In this code snippet, we've added a "title" prop with the value "Hello from Foo Page" to the "bar" layout within the "foo" page. This prop will be accessible in the "bar" layout.

Accessing Props in the Layout

To access the props passed from the page within the layout, you can use the useAttrs() function provided by Nuxt 3. Here's how you can do it in "bar.vue":

In the script setup section of "bar.vue," we use useAttrs() to access the props passed to the layout. In this case, we're logging the value of the "title" prop, which was set in the "foo" page.

Read also : Creating Nested Layouts in Nuxt 3: A Step-by-Step Guide.


Passing and accessing props in nested layouts with Nuxt 3 is a powerful technique that allows you to make your layouts dynamic and adaptable to different pages' requirements. By passing props from pages to layouts, you can customize the behavior and appearance of your layouts on a per-page basis, enhancing code reusability and maintainability.

In this blog post, we've explored how to pass props from a page to a nested layout and access those props within the layout using the useAttrs() function. This knowledge can be a valuable addition to your Nuxt 3 toolkit as you continue to build complex web applications with a structured and modular approach.