I'm developing a website where you can create, view and like playlists of movies.
In the "playlist page", which has a "/playlistid" route there is a checkbox that I use as a "like button".
Basically I load an array from my server (let's call it "favorites") with the IDs that I take from this.$route.params.id of the single pages, when you click like.
Now, how do I bind the v-model with my local (loaded) array, to check if $route.params.id is included inside, and make the input checked if this last check is true?
This is my code.
HTML side:
<label v-if="like" class="ml-2">
<input type="checkbox" class="heart" @change="checkLike" v-model='favorites.includes(this.$route.params.id)'>
<span></span>
</label>
SCRIPT side:
data () {
return {
favorites: []
}
},
methods: {
checkLike() {
if (this.favorites.includes(this.$route.params.id)) {
this.favorites.splice(this.$route.params.id, 1);
axios.patch(...)
} else if (!this.favorites.includes(this.$route.params.id)) {
this.favorites.push(this.$route.params.id)
axios.patch(...)
}
}
Using contains() or indexOf() inside v-model clearly doesn't work, so how could I do this? Thanks in advance for the help.