css:

这样写会报错

<view :class="getColor(1)">

  </view

解决:

<view :class="[getColor(1)]">

  </view

methods: {
  getColor(index) {
    return index  === 1 ? 'color1' : 'color2';
  }
}
.color1 {
  color: red;
}
.color2 {
  color: green;
}

style:

这样写会报错

<view :style="getColor(1)">

  </view

解决:用计算 computed

<view :style="getColor(1)">

  </view

computed: {
  getColor() {
     return (index) => {
        let style = {};
        index  === 1
          ?  style.borderColor = 'red'
          : style.borderColor = 'green'
        return style;
      }
  }
}

更多推荐