HTML之Vue框架v-for、v-if的简单使用

HTML之Vue框架 v-for、v-if 的简单使用

目标

数据定义一个数组,通过对v-for的使用,实现如下界面
在这里插入图片描述
使用v-if,使上述的奇数行不出现,效果如下图所示
在这里插入图片描述

代码

该代码可在我的GitHub中找到,链接在此

index1.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>v-for的使用</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
    <p v-for="(value, index) in sites">{{'索引值:' + index + ' --- 每一项:' + value}}</p>
</div>

<script>
    new Vue({
        el: '#app',
        data: {
            sites: [1, 2, 3, 4, 5, 6]
        }
    })
</script>
</body>
</html>

index2.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>v-if的使用</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
    <div v-for="(value, index) in sites">
        <p v-if="index % 2 != 0">
            {{'索引值:' + index + ' --- 每一项:' + value}}
        </p>
    </div>
</div>

<script>
    new Vue({
        el: '#app',
        data: {
            sites: [1, 2, 3, 4, 5, 6]
        }
    })
</script>
</body>
</html>

结果展示

index1.html 的结果图:
在这里插入图片描述
index2.html 的结果图:
在这里插入图片描述

发布了20 篇原创文章 · 获赞 22 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43198568/article/details/104363074