【vue】v-for

v-for遍历数组

在模板中使用v-for

v-for="item in items,同v-for="item of items"

//helloworld.vue
<template>
    <ul>
    <!-- <li v-for="city in cities" v-bind:key="city">{{city}}</li> -->
    <li v-for="(city,idx) in cities" v-bind:key="city">{{city}}-{{idx}}</li>
    </ul>
</template>

<script>
export default {
    data(){
        return {
            cities:['深圳','杭州','南昌']
        }
    }
}
</script>
使用render函数
//helloworld.vue
<template>
    <Helloworld/>
</template>

<script>
export default {
    components:{
        "Helloworld":{
            render:function(createElement){
                return createElement(
                    'ul',
                    // this.cities.map(
                    //     city => createElement(
                    //         'li',
                    //         {
                    //             key:city
                    //         },
                    //         city
                    //     )
                    // )
                    this.cities.map(
                        (city,idx) => createElement(
                            'li',
                            {
                                key:city
                            },
                            `${city}-${idx}`
                        )
                    )
                )
            },
            data:function(){
                return {
                    cities:['深圳','杭州','南昌']
                }
            }
        }
    }
}
</script>

v-for遍历对象

在模板中使用v-for
//helloworld.vue
<template>
    <ul>
        <li v-for="(value,key,idx) of obj" 
            v-bind:key="value">
            {{key}}-{{value}}-{{idx}}
        </li>
    </ul>
</template>

<script>
export default {
    data(){
        return {
            obj:{
                a:"hello",
                b:"world"
            }
        }
    }
}
</script>
使用render函数
//helloworld.vue
<template>
    <Helloworld/>
</template>

<script>
export default {
    components:{
        "Helloworld":{
            render:function(createElement){
                return createElement(
                    'ul',
                    Object.keys(this.obj).map(
                        (key,idx) => createElement(
                            'li',
                            {
                                key:key
                            },
                            `${key}-${this.obj[key]}-${idx}`
                        )
                    )
                )
            },
            data:function(){
                return {
                    obj:{
                        a:"hello",
                        b:"world"
                    }
                }
            }
        }
    }
}
</script>

v-for遍历一个整数

在模板中使用v-for
//helloworld.vue
<template>
    <ul>
        <li v-for="n in 10" v-bind:key="n">{{n}}</li>
    </ul>
</template>
// 依次输出:1 2 3 4 5 6 7 8 9 10
使用render函数
//helloworld.vue
<template>
    <Helloworld/>
</template>

<script>
export default {
    components:{
        "Helloworld":{
            render:function(createElement){
                return createElement(
                    'ul',
                    Array.apply(null,{length:10}).map(
                        (item,idx) => createElement(
                            'li',
                            {
                                key:idx+1
                            },
                            idx+1
                        )
                    )
                )
            }
        }
    }
}
</script>

v-for 包裹多个元素

<template>包裹多个元素,但在最终渲染的元素里不包含<template>元素。

在模板中使用v-for
//helloworld.vue
<template>
    <dl>
    <template v-for="(value,key) in obj">
        <dt v-bind:key="key">{{key}}</dt>
        <dd v-bind:key="value">{{value}}</dd>
    </template>
    </dl>
</template>

<script>
export default {
    data(){
        return {
            obj:{
                "鱼香肉丝":"鱼香肉丝里没有鱼",
                "夫妻肺片":"夫妻肺片里没有肺"
            }
        }
    }
}
</script>
使用render函数
//helloworld.vue
<template>
    <Helloworld/>
</template>

<script>
export default {
    components:{
        "Helloworld":{
            render:function(createElement){
                return createElement(
                    'dl',
                    Object.keys(this.obj).map(
                        key =>  [
                            createElement('dt',{key:key},key),
                            createElement('dd',{key:this.obj[key]},this.obj[key])
                        ]
                    )
                )
            },
            data:function(){
                return {
                    obj:{
                        "鱼香肉丝":"鱼香肉丝里没有鱼",
                        "夫妻肺片":"夫妻肺片里没有肺"
                    }
                }
            }
        }
    }
}
</script>

v-for实例:Todos

//index.js
import Vue from "vue";
import Todos from "./todos.vue";
const vm = new Vue({
    render:createElement => createElement(Todos)
}).$mount('#root');
//todos.vue
<template>
<div>
    <form v-on:submit.prevent="handleSubmit">
        <input type="text" v-model="todoTitle" placeholder="请输入"/>
        <input type="submit" value="新增">
    </form>
    <ul>
        <TodoItem v-for="(todo,idx) in todos" 
                    v-bind:key="todo.id"
                    v-bind:title="todo.title"
                    v-on:removeTodo="handleRemove(idx)"
        />
    </ul>
</div>
</template>

<script>
const todos = [
    {id:1,title:"吃饭"},
    {id:2,title:"睡觉"},
    {id:3,title:"打豆豆"}
]
let nextId = 4;

import TodoItem from "./todoItem.vue";
export default {
    data:function(){
        return {
            todos,
            nextId,
            todoTitle:""
        }
    },
    methods:{
        handleRemove:function(idx){
            this.todos.splice(idx,1);
        },
        handleSubmit:function(){
            let title = this.todoTitle.trim();
            if(!title) return;
            let newItem = {
                idx:this.nextId++,
                title:title
            }
            this.todos.push(newItem);
            this.todoTitle = "";
        }
    },
    components:{
        TodoItem
    }
}
</script>
//todoItem.vue
<template>
    <li>
        <span>{{title}}</span>
        <button v-on:click="handleClick">删除</button>
    </li>
</template>

<script>
export default {
    props:{
        title:String
    },
    methods:{
        handleClick:function(){
            this.$emit("removeTodo");
        }
    }
}
</script>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,v-for是一个重要的指令,用于循环渲染数组或对象的数据。v-for可以用于遍历数组和对象,并根据每个元素生成相应的DOM元素。 在引用中的代码示例中,v-for指令用于遍历一个对象obj的属性,并将每个属性的键值对渲染为<li>元素。通过使用v-for="(val, key) in obj",可以在模板中访问每个属性的值和键。 在引用中的代码示例中,v-for指令用于循环渲染数字。通过使用v-for="num in 9",可以循环遍历数字1到9,并将每个数字渲染为<li>元素。 在引用中的代码示例中,v-for指令用于遍历数组list,并将数组的每个元素渲染为<li>元素。通过使用v-for="(item, index) in list",可以在模板中访问每个数组元素的值和索引。 总结来说,v-for指令在Vue中用于循环遍历数组和对象,并根据每个元素生成相应的DOM元素。 参考资料: 引用的代码示例 引用的代码示例 引用的代码示例<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [vue中的v-for循环](https://blog.csdn.net/PCthedream/article/details/126980543)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值