Home > Web Front-end > Vue.js > What is the usage of vue bus?

What is the usage of vue bus?

藏色散人
Release: 2021-10-26 14:13:28
Original
8999 people have browsed it

Usage of vue bus: 1. Install vue-bus through "npm install vue-bus"; 2. Introduce vue-bus in main.js; 3. Through "this.$bus.emit(. ..);" and other methods can be used in the component.

What is the usage of vue bus?

The operating environment of this article: Windows 7 system, Vue2.9.6, Dell G3 computer.

What is the usage of vue bus?

How to use Bus in Vue:

Usage: pass values ​​between sibling components;

Installation:

npm install vue-bus
Copy after login

Introduce vue-bus in main.js

import Vue from 'vue';
import VueBus from 'vue-bus';
 
Vue.use(VueBus);
Copy after login

In the component Used in:

A component triggers events:

this.$bus.emit("parameterName",params);
Copy after login

B component receives events:

this.$bus.on("parameterName",function(value){
  console.log(value)
});
Copy after login

Unbind the event when the component is destroyed:

destroyed:function(){
  this.$bus.off("parameterName")
}
Copy after login

Recommended: "The latest 5 vue.js video tutorial selections"

The above is the detailed content of What is the usage of vue bus?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template