当前位置:网站首页>Global event bus
Global event bus
2022-07-28 05:46:00 【White mouse zero】
hypothesis App There are components in A、B、C、D.App There is one outside that does not belong to any component X. We want to achieve D Put the data 666 Pass it to the component A.
First ,A to X Bind a name demo Custom events for , The callback function of the event is in the component A in .
secondly , Components D Just trigger X On the body demo Events can be .
because :X On the body demo Be triggered , that demo The callback corresponding to the event is also executed .
If ,D I want to 8 Pass it to the component B, What to do ?
First ,B to X Bind a name test Custom events for , The callback function of the event is in the component B in .
secondly , Components D Just trigger X On the body test Events can be .
- X Where should I add it ?
First ,X It should be visible to all components ( Use ), that X belong Vue Add .
stay main.js in :
// stay Vue Add X
Vue.prototype.x = {
a:1,b:2}
however , The code above is dead . We should be
// Construct a VueComponent
const Demo = Vue.extend({
})
// d Is the instance object of the component vc, and vc Can call $on,$off,$emit
const d = new Demo()
// stay Vue Add X
Vue.prototype.x = d
We can still use it vm To complete , Below this On behalf of vm example , And it was completed before the production of the project .
new Vue({
……
beforeCreate(){
Vue.prototype.x = this
}
})
summary :
The standard writing is :
new Vue({
……
beforeCreate(){
Vue.prototype.$bus = this // Install the global event bus
}
})
Instance analysis
Premise :
App Contains components Student And the component School, Belongs to the same level component . We want to make Student Send a 666 to School.
- First , stay main.js Written in Chinese X( The following is written in standard $bus Instead of )
- secondly ,School Want to get Student The content in , Then give it to me X The binding event hello.
- Last ,Student Trigger event inside hello Can .
Student in :
Design a button , Give the button a click event , Click on the trigger sendStudentName(), And then trigger hello
Trigger hello event , And transmission 666
<template>
<div>
<button @click="sendStudentName"> Give the students their names School Components </button>
</div>
</template>
……
methods:{
sendStudentName(){
this.$bus.$emit('hello',666)
}
School in :
School Bind events at load time hello, The value obtained is data
mounted(){
this.$bus.$on('hello',(data)=>{
console.log(' I am a School Components , Received the data ',data)
}),
beforeDestroy(){
this.$bus.$off('hello') // Unbundling hello event
}
边栏推荐
猜你喜欢

Thinking on Architecture Design (SSO design)

wangeditor(@4.7.15)-轻量级的富文本编辑器

Review of metallurgical physical chemistry --- electrodeposition and reduction process of metals

【博学谷学习记录】超强总结,用心分享 | 集合

链表实现增删查改

Mutual conversion between latex and word

蓝桥代码 翻硬币(我这样写也通过了,官网测试是不是有问题)

冶金物理化学复习 --- 冶金反应动力学基础与多相反应动力学特征

c语言:通过一个例子来认识函数栈帧的创建和销毁讲解

对极大似然估计、梯度下降、线性回归、逻辑回归的理解
随机推荐
DOM--事件链、事件冒泡和捕获、事件代理
[idea plug-in artifact] teaches you how to set all attributes in an entity class with one click of idea
Idea uses dev tool to realize hot deployment
ArcGIS Engine开发资源
DOM操作的案例
Openjudge: campus accommodation reservation system
Microsoft Edge浏览器插件(2)
标准C语言学习总结8
ArcGIS地图制作的注记、格网添加
Review of metallurgical physical chemistry -- cathodic polarization, overpotential, anode and anode process in metal electrodeposition
结果填空 啤酒和饮料
GIS领域竞赛整理(不完全统计)
冶金物理化学复习 --- 复杂反应的速率方程
Database interview
ArcMap地图投影相关操作
基于XMind的E-R图制作
RESNET structure comparison
Openjudge: find the first character that appears only once
Review of metallurgical physical chemistry -- rate equations of complex reactions
Arcgis Engine安装的若干问题