Nuxt.js: Define layout dynamically

Created on 15 Feb 2017  Â·  21Comments  Â·  Source: nuxt/nuxt.js

Hi! Is there a way to define layout dynamically by variable or with a ternary expression? I need to change the layout if mobile device is detected.

This feature request is available on Nuxt.js community (#c230)
available soon enhancement

Most helpful comment

Small overview 😄

dynamic-layout

All 21 comments

Hi @Ca4tkin

Could you provide an example of how you would like to set a dynamic layout?

Something like this:
layout: this.$store.state.isMobile ? 'mobile' : 'default'

I am looking for the same solution.

Could you change layout property to a function that receive context, eg:

layout: (context) => context.isMobile ? 'mobile' : 'desktop'

+1 for the dynamic layout switching

+1

I will implement it for the next release.

Hi Guys!

Can we go further? Is it technically possible to add context to the layouts?

Because of no context now all the conditions needs to be put in the pages.
This causes code duplication in the pages and the layouts are only sufficient for very simple cases.

For example what I would like to do:

  • switching components depends on the user-agent, user location, language, headers etc
  • switching components depends on the application state
  • switching components depends on the external environments variables
  • switching themes (css) depends on the requested domains (domain.com, domain.fr, domain.de ...)
  • and so on.

Now all the code I moved to the pages and I don't use layouts at all :(

@alexchopin What do you think about it?

@awronski you can add all of these variables (location, user-agent, etc.) in the nuxtServerInit directly in your global state via the store.

Then, you can access $store everywhere in your layouts, pages and components.

The actual difficulty of setting a layout dynamically was the fact that a layout also have middleware attached to it, but I think I found a way of doing it.

@Atinux
Thanks, it works. I wrote an example. Maybe it will help someone else:

https://github.com/awronski/nuxtjs-examples/tree/master/mobile-desktop-with-store

Small overview 😄

dynamic-layout

The 0.10 release it out ✋

@kodekrendel
defined layout function at your vue file in pages folder

// index.vue
export default {
  layout () {
    return checkSomethingYouWant ? 'layout_a' : 'layout_b' ;
  }
}

https://nuxtjs.org/api/pages-layout/#the-layout-property

Yes, there is 2 examples: custom-layouts and dynamic-layouts, the live example and the documentation example use only custom-layouts.

See the live examples:

@kodekrendel

that because 0.10.0 have breaking change ,
aboult.vue should change data() to asyncData()
data function has no context anymore .

Fixed @kodekrendel @ausir0726

You can $watch the route and update the CSS classes applied, you don't need to use custom layouts for this @kodekrendel

I recommend you to carefully read the Vue.js and Vue-router documentation :)

Hi, is there any possibility to set the layout in middleware based on the route.
my some or pages will be always /admin/anything and some are domain.com/page
so for /admin/anything I want to use a different layout. this is currently possible by adding layout in each page but I want to set it once in middleware and it should be accessed automatically.

I have 4 or more different layouts. How would I set the layout dynamically to any one of the four based on a single variable ? The above allows for 2 layouts and I am struggling to implement it for more layouts. Any help would be greatly appreciated

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uptownhr picture uptownhr  Â·  3Comments

maicong picture maicong  Â·  3Comments

mattdharmon picture mattdharmon  Â·  3Comments

danieloprado picture danieloprado  Â·  3Comments

gary149 picture gary149  Â·  3Comments