1

How do I add my value of the prop thrID as a class value in the template?

thrID is passed in as my1value

<template>
  <div v-bind:class="['hhhhh',thrID]">
    test {{thrID}}
  </div>
</template>

<script>


export default {
  name: 'bottom',
  components: {

  },
  props:["thrID"]
}
</script>

<style scoped>
  .bottom {
    background: yellow;
    height: 30px;
    width: 100%;
  }
</style>

it renders

<div data-v-10e356bb="" data-v-7ba5bd90="" class="hhhhh">
  test my1value
</div>

I want it to have a class like this

<div data-v-10e356bb="" data-v-7ba5bd90="" class="hhhhh my1value">
  test my1value
</div>
1
  • Your code works fine. See this fiddle. Commented Apr 10, 2019 at 19:19

1 Answer 1

2

You can easily add custom classes by binding a string, array or object to the class attribute. You start of by using the v-bind or : syntax to bind a variable to the class attribute:

<template>
  <div :class="classes">
    <!-- Magic! -->
  </div>
</template>

Then, in our export component we can do several things. The most versatile option is to use an object. If the value of a key is truthy, that class is applied. If the value is falsy, it will not be applied. We use the [ keyName ]: value syntax to add this.thrID as a key to our object.

export default {
  props: {
    thrID: {
      type: String,
      required: true
    }
  },
  computed: {
    classes () {
      return {
        hhhhh: true,
        [this.thrID]: true
      }
    }
  }
}

Similarly, you can return an array:

classes () {
  return [
    'hhhhh',
    this.thrID
  ]
}

Or you can create some string with classes:

classes () {
  return `hhhhh {$this.thrID}`
}
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.