| TIP 
 注意我们不推荐在同一元素上使用 v-if 和 v-for。更多细节可查阅风格指南。
 
 当它们处于同一节点,v-if 的优先级比 v-for 更高,这意味着 v-if 将没有权限访问 v-for 里的变量:
 
 <!-- This will throw an error because property "todo" is not defined on instance. -->
 <li v-for="todo in todos" v-if="!todo.isComplete">
 {{ todo }}
 </li>
 可以把 v-for 移动到 <template> 标签中来修正:
 
 <template v-for="todo in todos">
 <li v-if="!todo.isComplete">
 {{ todo }}
 </li>
 </template>
 #在组件上使用 v-for
 这部分内容假定你已经了解组件相关知识。你也完全可以先跳过它,以后再回来查看。
 
 在自定义组件上,你可以像在任何普通元素上一样使用 v-for:
 
 <my-component v-for="item in items" :key="item.id"></my-component>
 然而,任何数据都不会被自动传递到组件里,因为组件有自己独立的作用域。为了把迭代数据传递到组件里,我们要使用 props:
 
 <my-component
 v-for="(item, index) in items"
 :item="item"
 :index="index"
 :key="item.id"
 ></my-component>
 不自动将 item 注入到组件里的原因是,这会使得组件与 v-for 的运作紧密耦合。明确组件数据的来源能够使组件在其他场合重复使用。
 
 下面是一个简单的 todo 列表的完整例子:
 
 <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')
 点击此处实现
 |