Home Web Front-end Vue.js How to use Vue to implement component communication?

How to use Vue to implement component communication?

Jul 17, 2023 pm 11:25 PM
vue component passing value vue component communication vue communication method

How to use Vue to implement component communication?

Vue is a popular JavaScript framework for building user interfaces. In Vue, components are the basic units for building web applications. And communication between components is critical to building complex applications. This article will introduce how to use Vue to implement communication between components and provide some code examples.

1. Parent component communicates with child components
Communication between parent component and child component is the most common scenario. Vue provides props attributes to implement this communication. In the parent component, data can be passed to the child component through props, and the child component can directly use the data in the props.

The following is a simple example of a parent component communicating to a child component:

Parent component

<template>
  <div>
    <h1>父组件</h1>
    <ChildComponent :message="message"></ChildComponent>
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: {
    ChildComponent
  },
  data() {
    return {
      message: 'Hello World!'
    };
  }
};
</script>
Copy after login

Child component

<template>
  <div>
    <h2>子组件</h2>
    <p>{{ message }}</p>
  </div>
</template>

<script>
export default {
  props: {
    message: {
      type: String,
      required: true
    }
  }
};
</script>
Copy after login

In the above example, the parent A data message is defined in the component, and the data is passed to the sub-component through the props attribute. Use props in the child component to receive the data passed by the parent component and render it in the template.

2. Communication from child components to parent components
Communication from child components to parent components is relatively complicated. Vue provides the $emit method to implement communication between child components and parent components.

The following is a simple example of a child component communicating to a parent component:

Parent component

<template>
  <div>
    <h1>父组件</h1>
    <ChildComponent @message="handleMessage"></ChildComponent>
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: {
    ChildComponent
  },
  methods: {
    handleMessage(message) {
      console.log(message);
    }
  }
};
</script>
Copy after login

Child component

<template>
  <div>
    <h2>子组件</h2>
    <button @click="sendMessage">发送消息</button>
  </div>
</template>

<script>
export default {
  methods: {
    sendMessage() {
      this.$emit('message', 'Hello World!');
    }
  }
};
</script>
Copy after login

In the above example, the child The message event is triggered in the component by using the $emit method, and the data Hello World! is passed to the parent component. The parent component uses @message to listen to the message event and handles the event in the handleMessage method.

3. Non-parent-child component communication
If you need to communicate between non-parent-child components, you can use the event bus mechanism provided by Vue. You can create an event bus instance and then communicate between components through this instance.

The following is an example of using event bus to implement non-parent-child component communication:

Event Bus

// eventBus.js
import Vue from 'vue';

const EventBus = new Vue();

export default EventBus;
Copy after login

Component A

<template>
  <div>
    <h2>组件A</h2>
    <button @click="sendMessage">发送消息</button>
  </div>
</template>

<script>
import EventBus from './eventBus.js';

export default {
  methods: {
    sendMessage() {
      EventBus.$emit('message', 'Hello World!');
    }
  }
};
</script>
Copy after login

Component B

<template>
  <div>
    <h2>组件B</h2>
    <p>{{ message }}</p>
  </div>
</template>

<script>
import EventBus from './eventBus.js';

export default {
  data() {
    return {
      message: ''
    };
  },
  created() {
    EventBus.$on('message', (message) => {
      this.message = message;
    });
  }
};
</script>
Copy after login

In the above example, an event bus instance EventBus is created, and then the message event is triggered in component A and the data Hello World! is passed to the event bus Example. Component B listens to the message event when it is created and updates the data after receiving the event.

Summary:
The above is a brief introduction on how to use Vue to implement component communication. In Vue, parent components can communicate with child components through props attributes, child components can communicate with parent components through the $emit method, and non-parent and child components can communicate through the event bus mechanism. I hope this article will help you understand component communication in Vue.

The above is the detailed content of How to use Vue to implement component communication?. 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)

How to pass value to vue component How to pass value to vue component Jan 06, 2023 pm 05:26 PM

Value passing method: 1. Use props to pass from parent to child; 2. From child to parent, you need to customize the event. Use "this.$emit('event name')" to trigger it in the child component, and use "@event" in the parent. name" monitoring; 3. Between brothers, use the public parent element as a bridge, combining parent and child props to pass parameters, and child and parent custom events; 4. Use routing to pass values; 5. Use $ref to pass values; 6. Use dependency injection to pass Descendants and great-grandchildren; 7. Use $attrs; 8. Use $listeners intermediate events; 9. Use $parent to pass, etc.

How to pass value to vue component How to pass value to vue component Jul 18, 2023 am 09:27 AM

The way to pass value in vue components: 1. Pass from parent to son; 2. Pass from son to father; 3. Pass value from brother; 4. Pass value with question mark, colon and parent-child component; 5. Use "$ref" to pass value; 6. Use "inject" to inject the data of the parent component into the current instance; 7. Pass from ancestor to grandson; 8. Pass from grandson to ancestor; 9. $parent; 10. Pass value to sessionStorage; 11. vuex.

Common problems and solutions for Vue component communication Common problems and solutions for Vue component communication Jul 17, 2023 pm 11:16 PM

Common problems and solutions for Vue component communication In Vue application development, component communication is a very important topic. Communication between different components can help us achieve functions such as data sharing, state management, and event delivery. However, component communication often encounters some problems. How to solve these problems is what we need to focus on during development. 1. Parent component passes data to child component A common scenario is that parent component needs to pass data to child component. In this case, we can use attribute binding to pass.

Implementation method of component parent-child value transfer function in Vue document Implementation method of component parent-child value transfer function in Vue document Jun 20, 2023 am 11:12 AM

Vue is a popular JavaScript framework. Its component development can help us improve the degree of modularity when developing complex applications, and improve the maintainability and scalability of the code. In Vue, data transfer between components is a very common requirement, and the most common scenario is data transfer between parent and child components. In order to implement this kind of data transfer in Vue, we need to understand the implementation method of value transfer between parent and child components. In Vue components, a parent component can have multiple child components at the same time, and the parent component can

What are the ways to pass values ​​in vue components? What are the ways to pass values ​​in vue components? Jun 19, 2023 pm 03:23 PM

The way Vue components pass values: 1. Pass from parent to child, define "props" in the child component to receive it; 2. Pass from child to parent, use "$emit()" to trigger in the child component; 3. Pass values ​​from brothers, define the event bus "eventBus"; 4. Question mark to pass value, colon to pass value and parent-child component to pass value; 5. Use "$ref" to pass value; 6. Use "inject" to inject the data of the parent component into the current instance; 7. Pass from ancestor to grandchild; 8 , Sun Chuanzu; 9. $parent; 10. sessionStorage value passing; 11. vuex.

Comparison of page jump solutions in Vue component communication Comparison of page jump solutions in Vue component communication Jul 17, 2023 pm 02:12 PM

Comparison of page jump solutions in Vue component communication In Vue development, page jump is one of the needs we often encounter. However, in component communication, page jumps need to consider issues such as data transfer and status management between components. This article will compare and analyze page jump solutions in Vue component communication, and give corresponding code examples. 1. Jump through routing Vue provides vue-router to manage the routing jump of the page. Page switching between components can be achieved through routing jumps, and parameters can be carried

An article briefly analyzing the problem of value transfer between parent and child components in Vue An article briefly analyzing the problem of value transfer between parent and child components in Vue Feb 23, 2023 pm 07:32 PM

How to pass values ​​between Vue parent and child components? The following article will take you through the value transfer issues of parent components and child components in Vue. I hope it will be helpful to you!

How to implement cross-level component communication in Vue? How to implement cross-level component communication in Vue? Jul 19, 2023 pm 08:45 PM

Vue.js is a popular JavaScript framework widely used for building user interfaces. In Vue's architecture, components are the basic building blocks, which can split a complex page into multiple reusable, independent components. Communication between these components is an important concept in Vue. This article will introduce how to implement cross-level component communication in Vue and provide some code examples. I hope it can help readers better understand the communication methods between components in Vue. In Vue, the flow of data is top-down, that is, from

See all articles