首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >vue 2如何在v-bind:class ternary中传递索引变量?

vue 2如何在v-bind:class ternary中传递索引变量?
EN

Stack Overflow用户
提问于 2017-08-02 04:26:58
回答 1查看 1.2K关注 0票数 1

我正在尝试使用索引(在v-for中)在vuejs中创建动态类,但似乎不能正常工作,有人能建议我怎么做吗?

代码语言:javascript
复制
v-bind:class="['GiallaSoto'+index ? 'minus' : !'GiallaSoto'+index, 'plus']" 

'GiallaSoto‘变量始终为真。然而,我声明它为false

代码语言:javascript
复制
    data: function(){
    return{
        servizioAggiunto : '',
        'GiallaDesc':false,
        'GiallaTutti':false,
        'GiallaSoto0':false,
        'GiallaSoto1':false,
        'GiallaSoto2':true,
        'GiallaSoto3':false,
    }
}

我想是与正确的中国税有关的东西。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-02 04:42:08

使用object语法,并结合使用方法从数据中获取正确的值。

代码语言:javascript
复制
console.clear()


new Vue({
  el:"#app",
  data: function(){
    return{
      servizioAggiunto : '',
      'GiallaDesc':false,
      'GiallaTutti':false,
      'GiallaSoto0':false,
      'GiallaSoto1':false,
      'GiallaSoto2':true,
      'GiallaSoto3':false,
    }
  },
  methods:{
    getClass(val, index){
      return {
        minus: !this['GiallaSoto'+index],
        plus: this['GiallaSoto'+index]
      }
    }
  }
})
代码语言:javascript
复制
.minus{
  background-color: red;
}
.plus{
  background-color: green;
}
代码语言:javascript
复制
<script src="https://unpkg.com/vue@2.2.6/dist/vue.js"></script>
<div id="app">
  <div v-for="index in 4" 
       :class="getClass('GiallaSoto', index - 1)" >
    {{index}}
  </div>
</div>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45447105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档