0

I have component named head.vue

 <b-navbar toggleable="lg" type="dark">

    <b-navbar-brand href="#"><img src="logo.png" class='logo'/></b-navbar-brand>

  </b-navbar>

What i want to do is only show logo on certain pages e.g.(homepage,about,privacy) and if not homepage i want to show title, see example:

<b-navbar toggleable="lg" type="dark">

        <b-navbar-brand href="#"><h1>Lorem Ipsum</h1></b-navbar-brand>

      </b-navbar>

How do i do this with conditional rendering? I can do this with components but want to know if possible to do on same component. For example:

 <b-navbar toggleable="lg" type="dark">
        <b-navbar-brand href="#" v-if="homepage"><h1>Lorem Ipsum</h1></b-navbar-brand>
        <b-navbar-brand href="#" v-else><h1>Lorem Ipsum</h1></b-navbar-brand>
</b-navbar>
8
  • I am not getting what you mean by "I can do this with components but want to know if possible to do on same component." Commented Jun 5, 2020 at 5:12
  • i mean i can just create a component showing only title and not logo. But i want to know if i can do this with conditional rendering? Commented Jun 5, 2020 at 5:13
  • You are already using conditional rendering with v-if and v-else. Commented Jun 5, 2020 at 5:15
  • but how do i use conditional rendering to see what page i am on e.g. localhost/homepage or localhost/about? Commented Jun 5, 2020 at 5:16
  • Are you using vue-router? Commented Jun 5, 2020 at 5:17

1 Answer 1

1

Add some extra data to the routes you want to show the logo on, eg

routes: [{
  path: '/homepage',
  component: HomePage,
  meta: { showLogo: true }
}, {
  path: '/about',
  component: About,
  meta: { showLogo: true }
}, {
  path: '/some-other-page',
  component: SomeOtherComponent
  // no meta required here
}]

Then in your head component, you can simply use

<b-navbar toggleable="lg" type="dark">
  <b-navbar-brand href="#">
    <img v-if="showLogo" src="logo.png" class='logo'/>
    <h1 v-else>Lorem Ipsum</h1>
  </b-navbar-brand>
</b-navbar>
computed: {
  showLogo () {
    return this.$route.meta.showLogo
  }
}

See https://router.vuejs.org/guide/advanced/meta.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.