Article From:https://segmentfault.com/q/1010000011135538
Question:

The problem is that the original vue1.x can be written as follows:

<el-carousel-item v-for="(item, key, index) in picList" :key="index" >
                <el-button id="carouselBtn" type="text" v-on:click="openDownLoad($index)" >
                    <img :src="item.img" :alt="item.altstring" class="img-responsive">
                </el-button>
 </el-carousel-item>

Since $index is abandoned in vue2.x, how do I get index when I use vue2.x?

Write index directly after clicking undefined

Solving?

Answer 0:

(item, index)in pcList , index It’s the index, and you don’t need to add $

Answer 1:
<el-carousel-item v-for="(item,index) in picList" >
                <el-button id="carouselBtn" type="text" v-on:click="openDownLoad(index)" >
                    <img :src="item.img" :alt="item.altstring" class="img-responsive">
                </el-button>
 </el-carousel-item>
 
 methods:{
     fun () {
        openDownLoad (index) {
            console.log(index)
        } 
     }
 }

Answer 2:
<el-carousel-item v-for="(item, index) in picList" :key="index" >
                <el-button id="carouselBtn" type="text" v-on:click="openDownLoad(index)" >
                    <img :src="item.img" :alt="item.altstring" class="img-responsive">
                </el-button>
 </el-carousel-item>

Answer 3:

vue2It is directly v-for= (“item, index) in picList”, which is directly transmitted to index in the event.
<el-button id=”carouselBtn” type=”text” v-on:click=”openDownLoad(index)” >

Answer 4:

Direct openDownLoad (index) is OK.

Answer 5:

Just pass index directly. $index is the grammar of Vue 1, and it’s out of date.

Answer 6:

Now it is key that has been deleted. Become dispensable.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *