Vue bus mechanism (bus)In addition to using vuex, communication between non-parent-child components in vue can also be done through the bus, and the two are applicable to different scenarios. The bus is suitable for small projects and projects where data is used by fewer components. It is not suitable for medium and large projects where data is used among many components. Bus is actually a publish-subscribe model. It uses Vue's custom event mechanism to publish an event through $emit at the triggering location, and listens to the event through $on on the page that needs to be listened to. Vuex is suitable for medium and large projects and situations where data is shared among multiple components. Use of component communication busCreate bus.js under the utils file // utils - bus.js import Vue from 'vue' const bus = new Vue() export default bus 1. Passing ValuesSend Message import bus from '@/utils/bus' The first parameter is the flag variable, and the second parameter is the communication value. us.$emit('message', 'hello'); Receiving information import bus from '@/utils/bus' The first parameter is the flag variable, and the e in the second parameter is the communication value. bus.$on('message', (e) => { console.log(e) }) 2. Calling methodOne component (A) calls a method of another component (B) Methods of component B import bus from '@/utils/bus' mounted () { bus.$on('testA', this.testA) }, testA () { console.log('Called by component A') } A component call import bus from '@/utils/bus' mounted () { bus.$emit('testA') } This is the end of this article about the use of bus in Vue. For more relevant content on the use of vue bus, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Introduction to version management tool Rational ClearCase
>>: Install docker offline by downloading rpm and related dependencies using yum
dl:Definition list Definition List dt:Definition t...
Google's goal with Flutter has always been to...
Today we will talk about how to use Jenkins+power...
Display Definition ID When the auto-increment ID ...
Preface: Vue3 has been released for a long time. ...
Table of contents 1. Download 2. Deployment 3. Ng...
offset Offset is the offset. Using the offset ser...
1. Basic Environment 1. Operating system: CentOS ...
This article shares the installation tutorial of ...
Carousel The main idea is: In the large container...
glibc is the libc library released by gnu, that i...
Basic syntax The use of text-overflow requires th...
This example uses jQuery to implement a mouse dra...
In actual Web development, inserting images, incl...
1. Stop the database server first service mysqld ...