Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js 在vuejs中单击添加类_Vue.js_Vuejs2_Vue Component_Vue Router - Fatal编程技术网

Vue.js 在vuejs中单击添加类

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

我的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="">
  </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)
},