Nuxt.js: conditional logic or props to layouts?

Created on 31 Jan 2017  路  2Comments  路  Source: nuxt/nuxt.js

First I really have to say I love what you do with the framework amazing just started yesterday and creating my first project with nuxt.js already!

I was wondering what will be the best approach on the following? this is my default layout:

<template>
  <div>
    <nav-bar />
    <nuxt/>
    <my-footer/>
  </div>
</template>

I want to reuse the layout but to add additional logic or props based on routes so it works really well for / but when ever I go to /search I want an extra prop for <nav-bar /> something like that:

<template> <div> <nav-bar :logo="true" /> <nuxt/> <my-footer/> </div> </template>

Because the routes are hidden/generated I'm not really sure what the best way to approach this without duplicating the layout?

Thanks a lot

This question is available on Nuxt.js community (#c152)
question

Most helpful comment

Hi @voidale

You can do it pretty easily by checking this.$route.path:

<template>
  <div>
    <nav-bar :logo="hasLogo" />
    <nuxt/>
    <my-footer/>
  </div>
</template>

<script>
export default {
  data () {
    return {
      hasLogo: false
    }
  },
  created () {
    this.routeChanged()
  },
  watch: {
    '$route': 'routeChanged'
  },
  methods: {
    routeChanged () {
      this.hasLogo = (this.$route.path === '/search')
    }
  }
}
</script>

All 2 comments

Hi @voidale

You can do it pretty easily by checking this.$route.path:

<template>
  <div>
    <nav-bar :logo="hasLogo" />
    <nuxt/>
    <my-footer/>
  </div>
</template>

<script>
export default {
  data () {
    return {
      hasLogo: false
    }
  },
  created () {
    this.routeChanged()
  },
  watch: {
    '$route': 'routeChanged'
  },
  methods: {
    routeChanged () {
      this.hasLogo = (this.$route.path === '/search')
    }
  }
}
</script>

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

VincentLoy picture VincentLoy  路  3Comments

vadimsg picture vadimsg  路  3Comments

gary149 picture gary149  路  3Comments

surmon-china picture surmon-china  路  3Comments

o-alexandrov picture o-alexandrov  路  3Comments