Home> Web Front-end> Vue.js> body text

How to use event bus functions in Vue documentation

WBOY
Release: 2023-06-21 08:53:15
Original
867 people have browsed it

How to use the event bus function in the Vue documentation

In Vue development, we often need to transfer data or call methods between different components. The event bus provides a simple and flexible way to implement communication between components.

The event bus is part of the Vue instance that allows components to communicate with each other. Simply put, the event bus is a Vue instance that can be used for communication between components. In other words, we can use the event bus to realize information transfer and method calling between components.

Usage:

The first step in using the event bus is to instantiate it in a Vue instance. We can instantiate an event bus in the main.js file and mount it on the Vue prototype.

import Vue from 'vue' Vue.prototype.$bus = new Vue()
Copy after login

In the above code, we added a$busobject to the Vue instance throughVue.prototype.and assigned it to a new one Vue instance. In this way, we can communicate between components through the$busobject.

Use the event bus to publish and subscribe to events

Next we can use the$busobject to publish and subscribe to events. We can use the$bus.$on()method in the component that needs to subscribe to the event to subscribe to the event. When the event is triggered, the callback function will be executed.

For example, an event namedfoois subscribed to in component A:

this.$bus.$on('foo', (msg) => { console.log(msg) })
Copy after login

Thefooevent with the same name is triggered in component B :

this.$bus.$emit('foo', 'this is message from component B')
Copy after login

This will outputthis is message from component Bin the console of component A.

Use the event bus to call methods

In addition to publishing and subscribing events, we can also use the$bus.$emit()method to perform methods call. This method calling method is also called function calling.

Define ahandleClick()method in component A:

methods: { handleClick(msg) { console.log(msg) } }
Copy after login

Use the$bus.$emit()method call in component BhandleClick()method in component A:

this.$bus.$emit('handleClick', 'this is a test message')
Copy after login

This will outputthis is a test messagein the console of component A.

Summary:

By using the event bus, we can easily publish and subscribe events, and call methods between Vue components. The event bus has a wide range of application scenarios in Vue development and can help us better organize and manage communication between components.

The above is the detailed content of How to use event bus functions in Vue documentation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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