Home Backend Development PHP Tutorial Vue component communication: using $emit to trigger child component events

Vue component communication: using $emit to trigger child component events

Jul 08, 2023 pm 03:04 PM
$emit vue component communication child component events

Vue component communication: Use $emit to trigger sub-component events

In Vue development, component communication is a very important topic, because data transfer and interaction between components are the key to building complex applications . Vue provides a variety of ways to implement communication between components, one of which is to use $emit to trigger sub-component events. In this article, we will introduce how to use $emit for component communication in Vue and deepen the understanding through sample code.

First of all, we need to understand the basic usage of $emit. In Vue, each component can trigger a custom event through the $emit method. This event can be listened to in the parent component and respond accordingly. The $emit method accepts two parameters. The first parameter is the name of the event to be triggered, and the second parameter is the value to be passed to the event handling function. Here is an example:

// 子组件
Vue.component('child', {
  template: `
    <div>
      <button @click="triggerEvent()">点击触发事件</button>
    </div>
  `,
  methods: {
    triggerEvent() {
      this.$emit('custom-event', 'Hello World!');
    }
  }
});

// 父组件
Vue.component('parent', {
  template: `
    <div>
      <child @custom-event="handleEvent"></child>
    </div>
  `,
  methods: {
    handleEvent(value) {
      console.log(value); // 输出:Hello World!
    }
  }
});

// 应用程序
new Vue({
  el: '#app',
});
Copy after login

In the above code, the button click event in the child component will trigger a custom event named "custom-event" with "Hello World!" as a parameter Passed to the parent component. The event is listened to in the parent component and the received parameters are printed out in the event handler function.

Through this example, we can see that the child component triggers a custom event through the $emit method and passes the data to the parent component. This approach establishes a direct communication pipeline between parent and child components, making the data flow between components clearer and more controllable.

In addition to listening to the custom events of the child component through "@event name" in the parent component, we can also use the v-on directive to achieve the same effect. For example, the parent component can listen to the events of the child component like this:

<child v-on:custom-event="handleEvent"></child>
Copy after login

The effects achieved by these two methods are exactly the same, but the writing methods are slightly different.

In addition, $emit can also use modifiers to more precisely control the event delivery behavior. Commonly used modifiers are .stop, .prevent and .once. The .stop modifier is used to prevent the event from bubbling, the .prevent modifier is used to prevent the default event, and the .once modifier is used to trigger the event only once. The following is an example of using modifiers:

// 子组件
Vue.component('child', {
  template: `
    <div>
      <button @click.stop="triggerEvent()">点击触发事件</button>
    </div>
  `,
  methods: {
    triggerEvent() {
      this.$emit('custom-event', 'Hello World!');
    }
  }
});
Copy after login

In the above code, the .stop modifier prevents the event from bubbling, that is, the event will only be triggered in the child component and will not bubble to Parent component.

To summarize, using $emit to trigger subcomponent events is a common way to implement component communication in Vue. Through the $emit method, the child component can trigger a custom event and pass the data to the parent component, thus realizing data transfer and interaction between components. During development, we can use different modifiers according to needs to more precisely control the event delivery behavior. We hope that through the introduction and sample code of this article, readers can better understand and use the $emit method to implement communication between Vue components.

The above is the detailed content of Vue component communication: using $emit to trigger child component events. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue component communication: using callback functions for component communication Vue component communication: using callback functions for component communication Jul 09, 2023 pm 07:42 PM

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

Vue component communication: use v-cloak directive to initialize display communication Vue component communication: use v-cloak directive to initialize display communication Jul 09, 2023 pm 08:10 PM

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: using $on for custom event listening Vue component communication: using $on for custom event listening Jul 08, 2023 pm 01:37 PM

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 Vue component communication: using $watch for data monitoring Jul 07, 2023 am 11:09 AM

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 component communication: Use the v-model directive for form two-way binding communication Jul 07, 2023 pm 03:03 PM

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

Design patterns for communication between Vue.js components Design patterns for communication between Vue.js components Sep 02, 2023 am 11:45 AM

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 v-bind directive for data transfer Vue component communication: use v-bind directive for data transfer Jul 07, 2023 pm 04:46 PM

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:

Vue error: The $emit method cannot be used correctly to dispatch custom events. How to solve it? Vue error: The $emit method cannot be used correctly to dispatch custom events. How to solve it? Aug 21, 2023 pm 10:15 PM

Vue error: The $emit method cannot be used correctly to dispatch custom events. How to solve it? In the Vue framework, we often encounter situations where custom events are needed for communication between components. Vue provides the $emit method for dispatching custom events, and communication can be achieved by listening to custom events of child components in the parent component. However, sometimes we may encounter the problem of being unable to correctly use the $emit method to dispatch custom events. This article will explore the solution to this problem. First, let's do

See all articles