Vue-for遍历

index.html

<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://unpkg.com/vue"></script>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <!--vue-app是根容器-->
    <div id="vue-app">
        <h1>V-if 循环</h1>
        <!--数组下标-->
        <!--{{characters[0]}}-->
        <!--{{characters[1]}}-->
        <!--{{characters[2]}}-->
        <!--数组遍历-->
        <ul>
            <li v-for="character in characters">
                {{character}}
            </li>
        </ul>
        <ul>
            <!--拿到下标-->
            <!--<li v-for="(user,index) in users">-->
            <!--{{index}}-->
            <li v-for="user in users">
                {{user.name}}-{{user.age}}
            </li>
        </ul>


        <!--遍历div-->
        <!--优化的话就是吧div还成template 这样渲染出来就不是很多块了-->
        <template v-for="(user,index) in users">
            <h3>{{index}}.{{user.name}}</h3>
            <p>Age-{{user.age}}</p>
        </template>
        <br>
        <!--深层遍历,先拿到对象,然后在遍历对象里面的元素-->
       <template v-for="(user,index) in users">
            <div v-for="(val,key) in user">
                <p>{{key}}-{{val}}</p>
            </div>
        </template>

        <!--在最新的vue不设置key值会报错,一般就用index-->
        <tbody v-for="(user,index) in users" :key="index">

        </tbody>


    </div>
    <script src="app.js"></script>
</body>
</html>

app.js

new Vue({
    el:"#vue-app",
    data:{
        characters:["Mario","Luffy","Yoshi"],
        users:[
            {name:"Henry",age:30},
            {name:"Bucky",age:25},
            {name:"Emily",age:18}
        ]
    },
    methods:{
    },
    computed:{
    }
});

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值