


Vue component communication: use v-for directive for list rendering communication
Vue component communication: Use the v-for directive for list rendering communication
In Vue.js, component communication is a very important part. One of the commonly used component communication methods is to use the v-for instruction for list rendering communication. Through the v-for directive, we can easily render a list and communicate between components in the list.
Example scenario:
Suppose we have a TodoList component that needs to render a to-do list and be able to implement the functions of adding, completing, and deleting items. Each item in the list is a separate component, and we want these components to communicate with each other.
Code implementation:
First, we need to create a TodoItem component to render the content of each to-do item. In this component, we can use the props attribute to receive data passed by the parent component.
<template> <div class="todo-item"> <input type="checkbox" v-model="isChecked" @change="completeTask"> <span :class="{ 'completed': isChecked }">{{ item }}</span> <button @click="deleteTask">删除</button> </div> </template> <script> export default { props: ['item'], data() { return { isChecked: false }; }, methods: { completeTask() { this.isChecked = !this.isChecked; }, deleteTask() { this.$emit('delete-task', this.item); } } }; </script> <style scoped> .completed { text-decoration: line-through; } </style>
Then, we need to use the v-for directive in the parent component to render the to-do list and communicate with the child component.
<template> <div> <h1>Todo List</h1> <input type="text" v-model="newTask" @keydown.enter="addTask"> <div class="todo-list"> <todo-item v-for="(task, index) in tasks" :key="index" :item="task" @delete-task="deleteTask" /> </div> </div> </template> <script> import TodoItem from './TodoItem.vue'; export default { components: { TodoItem }, data() { return { tasks: [], newTask: '' }; }, methods: { addTask() { if (this.newTask.trim() !== '') { this.tasks.push(this.newTask); this.newTask = ''; } }, deleteTask(item) { const index = this.tasks.indexOf(item); if (index !== -1) { this.tasks.splice(index, 1); } } } }; </script> <style scoped> .todo-list { margin-top: 20px; } </style>
In the above code, we use the v-for directive to loop through rendering each TodoItem component and pass each item to the child component through the props attribute. When the delete button in the child component is clicked, the custom event will be triggered through the $emit method and the items to be deleted will be passed to the parent component.
Through such a simple code implementation, we can implement the functions of adding, completing and deleting to-do items, and the components can communicate with each other.
Summary:
By using the v-for directive for list rendering communication, we can manage our components more flexibly and facilitate communication between components. In actual development, we can flexibly use the v-for instruction according to actual needs to improve development efficiency.
The above is an example and explanation of using the v-for instruction for list rendering communication. I hope it will be helpful for you to understand Vue component communication!
The above is the detailed content of Vue component communication: use v-for directive for list rendering communication. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Vue component communication: using callback functions for component communication In Vue applications, sometimes we need to let different components communicate with each other so that they can share information and collaborate with each other. Vue provides a variety of ways to implement communication between components, one of the common ways is to use callback functions. A callback function is a mechanism in which a function is passed as an argument to another function and is called when a specific event occurs. In Vue, we can use callback functions to implement communication between components, so that a component can

In web applications, scrolling lists are a very common way to display data, while infinite scrolling lists are a way to dynamically load more data. It is not difficult to implement an infinite scrolling list in Vue. With some simple operations, we can easily implement an infinite scrolling list. Preparing the data First, we need to prepare the data to be displayed. Generally, this data is obtained through interfaces. In this example, we can use a fake data source to simulate obtaining data: constdata=[

Vue component communication: Use the v-cloak directive to initialize display communication. In Vue development, component communication is a very important topic. Vue provides a variety of communication methods, among which using the v-cloak directive to initialize display communication is a common method. In this article, we will learn how to use v-cloak directives for communication between components and explain it in detail with code examples. First, let's understand what the v-cloak instruction does. The v-cloak directive is a Vu

Vue component communication: Use $on for custom event listening. In Vue, components are independent, and each component has its own life cycle and data. However, in the actual development process, communication between components is inevitable. Vue provides a very flexible and efficient way of component communication: custom event listening. Vue's custom event listening mechanism is implemented based on the publish-subscribe model. You can listen to a custom event in a component by using the $on method of the Vue instance, and use the $emit method in

Vue component communication: using $watch for data monitoring In Vue development, component communication is a common requirement. Vue provides a variety of ways to implement communication between components. One of the common ways is to use $watch for data monitoring. This article will introduce the usage of $watch and give corresponding code examples. Vue's instance object provides the $watch method for monitoring data changes. $watch accepts two parameters: the property name of the data to be monitored, and the callback function. When listening to data

Vue component communication: Use the v-model directive for form two-way binding communication Vue.js is a progressive JavaScript framework for building user interfaces that is lightweight, flexible and efficient. In Vue applications, component communication is a very important feature. Vue provides a variety of ways to implement communication between components, among which using the v-model directive for form two-way binding communication is a common and convenient way. In Vue, the v-model directive is used in forms

As developers, we want to produce code that is manageable and maintainable, which is also easier to debug and test. To achieve this, we employ best practices called patterns. Patterns are proven algorithms and architectures that help us accomplish specific tasks in an efficient and predictable way. In this tutorial, we'll look at the most common Vue.js component communication patterns, as well as some pitfalls we should avoid. We all know that in real life, there is no single solution to every problem. Likewise, in Vue.js application development, there is no universal pattern that applies to all programming scenarios. Each mode has its own advantages and disadvantages and is suitable for specific use cases. The most important thing for Vue.js developers is

Vue component communication: Use the v-bind instruction for data transfer. Vue.js is a popular front-end framework that provides powerful component development capabilities. In Vue applications, component communication is an important issue. The v-bind instruction is a data transfer method provided by the Vue framework. This article will introduce how to use the v-bind instruction to transfer data between components. In Vue, component communication can be divided into two situations: parent-child component communication and sibling component communication. Below we will introduce from these two aspects respectively:
