最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.js dynamic class name? - Stack Overflow

programmeradmin5浏览0评论

I need a color degradation depending on a review grade. I was hoping to get something done in Vue.js like so:

<div class="review" :style="reviewColor(hotel.average)">

And in my methods I have something like this:

reviewColor() {
    return 'green';
}

Unfortunately this does not provide me with a 'green' class. I was hoping to do my color calculation in the method.

If the grade is less than a 7 it needs to be a specific color, if between 7 and 8 and higher than 8.

I need these calculations in a clean matter. Is there any alternative?

I can't inline it because I have 2 elements that need to respond to a class.

I need a color degradation depending on a review grade. I was hoping to get something done in Vue.js like so:

<div class="review" :style="reviewColor(hotel.average)">

And in my methods I have something like this:

reviewColor() {
    return 'green';
}

Unfortunately this does not provide me with a 'green' class. I was hoping to do my color calculation in the method.

If the grade is less than a 7 it needs to be a specific color, if between 7 and 8 and higher than 8.

I need these calculations in a clean matter. Is there any alternative?

I can't inline it because I have 2 elements that need to respond to a class.

Share Improve this question asked Jul 17, 2017 at 13:12 Stephan-vStephan-v 20.4k32 gold badges121 silver badges211 bronze badges 1
  • You need to specify the style aspect that you want to change. style="green" isn't valid. Try :style="{color:reviewColor(hotel.average)}" – RainingChain Commented Jul 17, 2017 at 13:24
Add a ment  | 

1 Answer 1

Reset to default 9

Unfortunately this does not provide me with a 'green' class.

You need to bind to class, not style:

<div class="review" :class="reviewColor(hotel.average)">
reviewColor(grade) {
  if (grade < 7) {
    return 'red';
  } else if (grade < 9) {
    return 'yellow';
  } else {
    return 'green';
  }
}
发布评论

评论列表(0)

  1. 暂无评论