I'm facing an issue with my computed attribute 'alphabet', which contains a list of objects, each with attributes letter (a string) and guessed (a boolean). I've been attempting to bind the guessed attribute to a class using Vue.$set instead of an equality operator, as per the documentation's suggestion. However, even after trying this, I can't seem to make it work.
Setting the guessed attribute to true by default did work, indicating that the problem doesn't lie in my CSS configuration.
To confirm that the value of the guessed attribute is changing, I printed out the alphabet object in the console (and it did change). The issue seems to be that, for some reason, the value isn't being updated reactively in the HTML.
Here is how I've configured the list:
<ul>
<li v-for="(letter, index) in alphabet"
v-bind:key="letter.id"
@click="makeGuess(index)"
v-bind:class="{guessed: letter.guessed}">{{letter.letter}}
</li>
</ul>
On the last line, I'm attempting to connect the guessed attribute with the class.
This is my alphabet attribute:
computed: {
alphabet: function () {
var objects = [];
var letters = "abcdefghijklmnopqrstuvwxyz".split("");
letters.forEach(function (element) {
objects.push({
letter: element,
guessed: false
})
});
return objects;
}
}
And here is my makeGuess function:
methods: {
makeGuess: function (index) {
if (this.word.includes(this.alphabet[index].letter)) {
this.$set(this.alphabet, index, {
letter: this.alphabet[index].letter,
guessed: true
})
console.log(this.alphabet)
}
}
}
The expected outcome is for the guessed class to be added reactively when letter.guessed is true, but it's not happening. What am I missing?