1、数组元素映射到列表元素
<ul id="array-rendering">
<li v-for="item in items">
{{ item.message }}
</li>
</ul>
Vue.createApp({
data() {
return {
items: [{ message: 'Foo' }, { message: 'Bar' }]
}
}
}).mount('#array-rendering')
v-for 支持第二个参数index
<ul id="array-with-index">
<li v-for="(item, index) in items">
{{ parentMessage }} - {{ index }} - {{ item.message }}
</li>
</ul>
Vue.createApp({
data() {
return {
parentMessage: 'Parent',
items: [{ message: 'Foo' }, { message: 'Bar' }]
}
}
}).mount('#array-with-index')
2、Object 映射到列表元素
<ul id="v-for-object" class="demo">
<li v-for="value in myObject">
{{ value }}
</li>
</ul>
Vue.createApp({
data() {
return {
myObject: {
title: 'How to do lists in Vue',
author: 'Jane Doe',
publishedAt: '2016-04-10'
}
}
}
}).mount('#v-for-object')
#name
<li v-for="(value, name) in myObject">
{{ name }}: {{ value }}
</li>
#index
<li v-for="(value, name, index) in myObject">
{{ index }}. {{ name }}: {{ value }}
</li>
添加key 有助于性能优化
<div v-for="item in items" :key="item.id">
<!-- content -->
</div>
3、数组值改变的方式
Vue会观察数组变化从而触发视图更新。
push()
pop()
shift()
unshift()
splice()
sort()
reverse()
下面方法不会改变原数组会返回一个新数组。
filter()
concat()
slice()
Vue实现了一些智能开发,最大化DOM元素重用,重新赋值的数组并不是重新创建dom 而是尽可以能使用原有DOM
example1.items = example1.items.filter(item => item.message.match(/Foo/))
如果只是想返回sort 或filter 数组,并不想改变原始数据可以用computed
<li v-for="n in evenNumbers">{{ n }}</li>
data() {
return {
numbers: [ 1, 2, 3, 4, 5 ]
}
},
computed: {
evenNumbers() {
return this.numbers.filter(number => number % 2 === 0)
}
}
如果计算属性不能满足条件,你可以使用method
<ul v-for="numbers in sets">
<li v-for="n in even(numbers)">{{ n }}</li>
</ul>
data() {
return {
sets: [[ 1, 2, 3, 4, 5 ], [6, 7, 8, 9, 10]]
}
},
methods: {
even(numbers) {
return numbers.filter(number => number % 2 === 0)
}
}
4、v-for 整形数值
数值是多少就返回多少次,下面例子循环10次,显示12345678910
<div id="range" class="demo">
<span v-for="n in 10">{{ n }} </span>
</div>
5、v-for 在 <template>
和v-if一样v-for 也可以用在template上,包多个元素
<ul>
<template v-for="item in items">
<li>{{ item.msg }}</li>
<li class="divider" role="presentation"></li>
</template>
</ul>
5、v-for 和 v-if
不推荐v-for 和v-if 同时使用。
<li v-for="todo in todos" v-if="!todo.isComplete">
{{ todo }}
</li>
6、v-for 用到 Component
<div id="todo-list-example">
<form v-on:submit.prevent="addNewTodo">
<label for="new-todo">Add a todo</label>
<input
v-model="newTodoText"
id="new-todo"
placeholder="E.g. Feed the cat"
/>
<button>Add</button>
</form>
<ul>
<todo-item
v-for="(todo, index) in todos"
:key="todo.id"
:title="todo.title"
@remove="todos.splice(index, 1)"
></todo-item>
</ul>
</div>
const app = Vue.createApp({
data() {
return {
newTodoText: '',
todos: [
{
id: 1,
title: 'Do the dishes'
},
{
id: 2,
title: 'Take out the trash'
},
{
id: 3,
title: 'Mow the lawn'
}
],
nextTodoId: 4
}
},
methods: {
addNewTodo() {
this.todos.push({
id: this.nextTodoId++,
title: this.newTodoText
})
this.newTodoText = ''
}
}
})
app.component('todo-item', {
template: `
<li>
{{ title }}
<button @click="$emit('remove')">Remove</button>
</li>
`,
props: ['title']
})
app.mount('#todo-list-example')
网友评论