列表渲染 v-for的使用

本文介绍了Vue.js中v-for指令的使用,强调了在使用v-for时绑定key属性的重要性,建议将ID作为key的值,确保其唯一性。同时,文章提到了索引作为key的不足,并提供了关于key值类型的注意事项。
摘要由CSDN通过智能技术生成
<table class="table table-bordered table-hover table-striped" >
            <thead>
                <th>索引</th>
                <th>Id</th> 
                <th>姓名</th> 
            </thead>
            <tbody> 
                <tr v-for="(item,index) in list" :key="item.id"  :title = 'item.name + index'>
                    <td>{{index}}</td>
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                </tr>
            </tbody>
        </table>
    </div>
    <script>
        let vm = new Vue({
           el:'#app',
            data:{
                username:"zhangsan",
                n1:1,
                n2:2,
                type:"J",
                list:[
                    {id: 1,name:'张三'},
                    {id: 2,name:'李四'},
                    {id: 3,name:"王麻"}
                ]
            },
            methods: {
                showName(){
                    console.log('用户名:'+this.username)
                }
            },
        })
    </script>

V-for中key值使用的注意点
建议:
1.只要用到了v-for指令,那么一定要绑定一个 :key属性
2.尽量把id作为key的值(当前循环项的id值)
3.key的值只能是字符串和数字类型(建议id值唯一)
4.索引不具有唯一性(没有意义)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值