search

Home  >  Q&A  >  body text

javascript - Problem with combining v-for and v-if in vue?

Use v-for to traverse N follow buttons, click one of the follow buttons, and the corresponding follow button becomes followed. This is the first time

 <img v-if='flag' @click='change()' :src='countries[num]' alt="">//关注
    <img v-if='!flag' :src='countriesHasAttention[num]' alt="">//  已关注
    data () {
        return {
            flag: true
        }
    }    
    change: function () {
        this.flag = false
    }
    

I found that clicking one changed everything, and then I changed the flag to an array

<img v-if='flag[index]' @click='change(index)' :src='countries[num]' alt=""> //关注
<img v-if='!flag[index]' :src='countriesHasAttention[num]' alt="">    //  已关注
data () {
        return {
            flag: [true, true, true]
        }
}   
change: function (index) {
        this.flag[index] = false
}

发现这样做点击的时候按钮不发生变化。
求大神指导一下
PHP中文网PHP中文网2773 days ago791

reply all(2)I'll reply

  • 仅有的幸福

    仅有的幸福2017-06-26 10:55:49

    The

    change part is changed to
    Vue.set

    change(index){
        Vue.set(this.flag,index,false)
    }

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-26 10:55:49

    The template can be simplified like this:

    <img @click="change(index)" :src="flag[index] ? countries[num] : countriesHasAttention[num]" alt="">

    The answer above for data processing is correct, see: Array Update Detection

    reply
    0
  • Cancelreply