当前位置:网站首页>全局事件总线
全局事件总线
2022-07-23 04:32:00 【道貌岸然304】
适用于任意组件
使用方法
1,先创建一个事件总线
要求得是所有组件都能访问到的地方,根据原型链这个地方就是Vue的原型对象身上
在Vue的原型对象上 创建一个全局事件总线
new Vue({
// 指定容器
el: '#app',
// 解析App模板
render: h => h(App),
// 创建一个全局事件总线
beforeCreate() {
Vue.prototype.$bus = this
}
})2,怎样使用
在接受数据的组件中绑定自定义事件,在发送数据中使用自定义事件
<script>
export default {
name: "School",
data() {
return {
name: "atguigu",
address: "北京",
stuName: "",
};
},
methods: {
getStuName(name) {
console.log("我是School", name);
this.stuName = name;
},
},
mounted() {
//绑定全局事件
this.$bus.$on("getStuName", this.getStuName);
},
// 解绑事件
beforeDestroy() {
this.$bus.$off("getStuName");
},
};
</script>在发送数据的组件中调用全局事件
<script>
export default {
name: "Student",
data() {
return {
name: "张三",
};
},
methods: {
send() {
console.log("fasong");
this.$bus.$emit("getStuName", this.name);
},
},
};
</script>
是自定义组件的一个提升
边栏推荐
- [warning] recognizing corrupt image/label during yolov5 training: [errno 2]...... it is impossible to complete the training data set. I will take you to solve it quickly
- 12 个适合做外包项目的开源后台管理系统
- China Economic Net: "Yuan universe" is hot
- Cs5266+ma8621 do the scheme design of typec to hdmi+pd+u3+2u+sd/tf seven in one expansion dock | cs5266 multi port expansion dock pcb+ schematic diagram reference
- PyQt5_pyqtgraph鼠标在折线图上画线段
- CV (3)- CNNs
- openvino_datawhale
- MySQL主从复制
- openvino_datawhale
- Seektiger's okaleido has a big move. Will the STI of ecological pass break out?
猜你喜欢
随机推荐
Unityc realizes the conversion of Chinese characters to Pinyin - using Microsoft chspinyinconv Library
PyQt5_QListWidget分页多选控件
Figure 8 sequence of crystal head connection of network cable
Sequence model (2) - natural language processing and word nesting
Clion + mingw64 configure C language development environment visual studio installation
Ultra Fast Deep Lane Detection with Hybrid Anchor Driven Ordinal Classification论文解读
0基础转行软件测试,月薪6000和11000的必备技能,截然不同...
China Economic Net: "Yuan universe" is hot
Richview textbox items textbox
Chapter2 Standard Output
Kingbasees SQL language reference manual of Jincang database (8. Function (III))
Kubernetes技术与架构(六)
CV (3)- CNNs
How to delete an item in sonar
CLion + MinGW64配置C语言开发环境 Visual Studio安装
What is file management software? Why do you need it?
SPR:SUPERVISED PERSONALIZED RANKING BASED ON PRIOR KNOWLEDGE FOR RECOMMENDATION
NFT数字藏品版权如何保护?
Chapter 4 Executing Commands
openvino_datawhale









