I need some help trying to figure out how to give the v-bind:class multiple options.
My Uno game, as it loops through your cards, needs to look at the cards color which is in a list of objects ex. ([{ Color: green, Value: 6}]
and determine what color of text the card should be.
This is what I have come up with so far:
Vue.js
JavaScript
x
16
16
1
getClass: function(card){
2
var result = [];
3
console.log(card);
4
if (card.Color == "red"){
5
result.push('red');
6
}else if (card.Color == "green"){
7
result.push('green');
8
}else if (card.Color == "blue"){
9
result.push('blue');
10
}else if(card.Color == "yellow"){
11
result.push('yellow');
12
}
13
console.log(result);
14
return result;
15
},
16
HTML
JavaScript
1
7
1
<ul id="myCards">
2
<button id="myCard" v-for="card in myCards" v-bind:class="getClass(card)"
3
@click="playCard(card)">
4
{{card.Color}} {{card.Value}}
5
</button>
6
</ul>
7
CSS
JavaScript
1
32
32
1
ul{
2
text-align: left;
3
}
4
#myCards{
5
padding: none;
6
}
7
#myCard{
8
display: inline-block;
9
height: 100px;
10
width: 70px;
11
border: 1px solid black;
12
color: black;
13
border: 2px solid black;
14
border-radius: 6px;
15
background-color: white;
16
color: black;
17
vertical-align: middle;
18
margin: 5px;
19
}
20
.red{
21
color: red;
22
}
23
.green{
24
color: green;
25
}
26
.blue{
27
color: blue;
28
}
29
.yellow{
30
color: yellow;
31
}
32
Advertisement
Answer
Since there’s one property called Color
inside the card object you could use style object syntax
and you don’t need to define classes for each color:
JavaScript
1
7
1
<ul id="myCards">
2
<button id="myCard" v-for="card in myCards"
3
@click="playCard(card)">
4
{{card.Color}} {{card.Value}}
5
</button>
6
</ul>
7
JavaScript
1
25
25
1
Vue.config.devtools = false;
2
Vue.config.productionTip = false;
3
4
let app = new Vue({
5
el: "#app",
6
7
data() {
8
return {
9
myCards: [{
10
Value: 'card 1',
11
Color: 'red'
12
},
13
{
14
Value: 'card 2',
15
Color: 'blue'
16
},
17
]
18
19
}
20
},
21
methods: {
22
23
24
}
25
})
JavaScript
1
21
21
1
ul {
2
text-align: left;
3
}
4
5
#myCards {
6
padding: none;
7
}
8
9
#myCard {
10
display: inline-block;
11
height: 100px;
12
width: 70px;
13
border: 1px solid black;
14
color: black;
15
border: 2px solid black;
16
border-radius: 6px;
17
background-color: white;
18
color: black;
19
vertical-align: middle;
20
margin: 5px;
21
}
JavaScript
1
8
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
2
<div id="app">
3
<ul id="myCards">
4
<button id="myCard" v-for="card in myCards" :style="{color:card.Color}">
5
{{card.Value}}
6
</button>
7
</ul>
8
</div>