4

I am very new to Vuejs, so please pardon me if this is something very common. But I am trying to style an item based on the condition of whether the name of the item is equal to another object.

<template>
  <div id="subMenuWrapper">
    <ul id="subMenuList">
      <li v-for="menu in SubMenuItems" v-bind:class="{activeSubMenuItem === menu.name? activeSubMenuItemStyle:subMenuItemStyle}">{{menu.name}}</li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'subMenuWrapper',
  data() {
    return {
      SubMenuItems: [
        {
          name: 'IN THEATERS',
        },
        {
          name: 'COMING SOON',
        },
      ],
      activeSubMenuItem: 'IN THEATERS',
    }
  },
}
</script>

<style>
.activeSubMenuItemStyle {
  font-weight: bold;
  color: #e4bb23;
  padding: 8px 0;
}

.subMenuItemStyle {
  font-weight: bold;
  color: #e4bb23;
  padding: 8px 0;
}
</style>

This is giving me an error:

Vue template syntax error:

  • invalid expression: v-bind:class="{activeSubMenuItem === menu.name? activeSubMenuItemStyle:subMenuItemStyle}"

How can I style an item in a list based on its properties?

Update:

Tried this way too:

<li v-for="menu in SubMenuItems" v-bind:class="[activeSubMenuItem === menu.name? activeSubMenuItemStyle:subMenuItemStyle]">{{menu.name}}</li>

and added the activeSubMenuItemStyle and subMenuItemStyle into the data with style properties.

This is not giving any errors, but the item has no styles

1
  • You have no style because your CSS is not being read, it can be something related to your build process. Remove the CSS from the template file and make it global. Commented Feb 20, 2017 at 12:24

1 Answer 1

3
  1. If you are using a ternary expression, you need to use different brackets:

<div v-bind:class="[isActive ? activeClass : '', errorClass]">

Should work after this correction.

Try to refer to docs: https://v2.vuejs.org/v2/guide/class-and-style.html#Array-Syntax . If it won't work - let me know, I'll try to edit answer to help.

  1. As pet Mat comment, <li v-for="menu in SubMenuItems" v-bind:class="[activeSubMenuItem === menu.name? 'activeSubMenuItemStyle':'subMenuItemStyle']">{{menu.name}}</li>
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I tried that way too <li v-for="menu in SubMenuItems" v-bind:class="[activeSubMenuItem === menu.name? activeSubMenuItemStyle:subMenuItemStyle]">{{menu.name}}</li>. Its not giving any errors, but its not having any styles.
activeSubMenuItemStyle, if this is the name of the css class, you should put that in quotes like a string. Anything inside the quotes of v-bind:class should be a valid javascript expression, in your case, they might be interpreted as undeclared variables. try v-bind:class="[activeSubMenuItem === menu.name? 'activeSubMenuItemStyle':'subMenuItemStyle']"
Kakar - please put jsfiddle together with your code to let us help you

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.