javascript - Problem with combining v-for and v-if in vue?
PHP中文网
PHP中文网 2017-06-26 10:53:49
0
2
779

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中文网

认证0级讲师

reply all(2)
仅有的幸福

The

change part is changed to
Vue.set

change(index){
    Vue.set(this.flag,index,false)
}
我想大声告诉你

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template