Vue.js 在vuejs中单击添加类
我的vue模板:Vue.js 在vuejs中单击添加类,vue.js,vuejs2,vue-component,vue-router,Vue.js,Vuejs2,Vue Component,Vue Router,我的vue模板: <div class="col-sm-4 col-xs-6 thumb" v-for="(photo, index) in photos" @click.prevent="check(index)" > <a class="thumbnail" :class="{'active': photo.checked}"> <img class="img-responsive" :src="photo.picture" alt
<div
class="col-sm-4 col-xs-6 thumb"
v-for="(photo, index) in photos"
@click.prevent="check(index)"
>
<a class="thumbnail" :class="{'active': photo.checked}">
<img class="img-responsive" :src="photo.picture" alt="">
</a>
</div>
一切似乎都是正确的,但不起作用。可能是什么问题?
获取传递给check()
的索引的photo
对象的引用,然后使用更新数组,如下所示:
check(index) {
let photo = this.photos[index];
if (!("checked" in photo)) {
photo.checked = true
} else {
photo.checked = !photo.checked
}
Vue.set(this.photos, index, photo);
},
只需单击@click.prevent=“$set(photo,'checked',!photo.checked)”
并忘记处理程序如何
<div class="col-sm-4 col-xs-6 thumb" v-for="(photo, index) in photos"
@click.prevent="$set(photo, 'checked', !photo.checked)">
<a class="thumbnail" :class="{'active': photo.checked}">
<img class="img-responsive" :src="photo.picture" alt="">
</a>
</div>
这也是我的第一个想法。我不知道为什么,但如果照片
最初没有选中的属性(在本例中似乎就是这种情况),则这不起作用@谢谢如果它最初没有属性,那么它需要通过$set进行设置。啊,是的,这很合理。我们可以开一个issue@ChadidiAbdellah不,这是预期的行为。
<div class="col-sm-4 col-xs-6 thumb" v-for="(photo, index) in photos"
@click.prevent="$set(photo, 'checked', !photo.checked)">
<a class="thumbnail" :class="{'active': photo.checked}">
<img class="img-responsive" :src="photo.picture" alt="">
</a>
</div>
<div class="col-sm-4 col-xs-6 thumb" v-for="(photo, index) in photos"
@click.prevent="check(photo)">
check(photo) {
this.$set(photo, 'checked', !photo.checked)
},