当前位置:网站首页>组件传值:兄弟间传值(非父子组件进行传值)
组件传值:兄弟间传值(非父子组件进行传值)
2022-06-21 22:04:00 【Nancy-sn】
非父子组件之间传值,需要定义个公共的公共实例文件bus.js,作为中间仓库来传值。
当调用send触发bus的aa自定义事件,bus上绑定的事件,一旦触发emit,bus.$on将被触发接收到msg赋给this.msg
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
</head>
<body>
<div id="app">
<tab1></tab1>
<tab2></tab2>
</div>
</body>
</html>
<script src="./js/vue.js"></script>
<script>
const bus= new Vue();//非父子传值(兄弟传值)
//兄弟组件1
Vue.component("tab1",{
template:`
<div>tab1 <button @click="send">点击</button> </div>
`,
data(){
return{
msg:"tab1"
}
},
methods:{
send(){
//出发自定义aa事件,携带了数据
bus.$emit("aa",this.msg);
}
},
});
//兄弟组件2
Vue.component("tab2",{
template:`
<div>tab2 {
{msg}}</div>
`,
data(){
return{
msg:""
}
},
//挂载的构造函数
mounted(){
//给hug绑定"aa"的事件
bus.$on("aa",(msg)=>{
this.msg=msg;
})
},
});
new Vue({
el: "#app",
data: {
}
})
</script>运行后:

当点击后:

边栏推荐
- Solve the problem that the letter of a key in laptop (I) cannot be pressed
- Redis master-slave replication (9)
- 同花顺VIP开户怎么开户,安全吗?
- Uni app advanced style framework / production environment [Day10]
- 布局路线图,空间布局与数据可视化的完美结合
- Inventory common vulnerability utilization methods
- 路由器连接上但上不了网是什么故障
- 逆向调试入门-基础知识
- Is it safe for the head teacher to open a stock account in qiniu?
- 在线文本按行批量反转工具
猜你喜欢
![Jmter test command [note]](/img/96/4290b92beb0755c6724a3b8d7dc635.png)
Jmter test command [note]

转载:网络加载框架 - Retrofit
![[数据库课程设计]基于Sql Server的教室信息管理系统(附部分源码)](/img/7e/47011ee2a35c50669a86fd5cf543d9.png)
[数据库课程设计]基于Sql Server的教室信息管理系统(附部分源码)

关于 安装Qt5.15.2启动QtCreator后“应用程序无法正常启动0xc0000022” 的解决方法

麒麟系统开发笔记(五):制作安装麒麟系统的启动U盘、物理机安装麒麟系统以及搭建Qt开发环境

关于 国产麒麟Qt编译报错“xxx.pri has modification time xxxx s in the futrue“ 的解决方法

Fork join thread pool

You have a chance, here is a stage

Nuxt SSR packaging and deployment

Notes on the development of raspberry pie (15): Raspberry pie 4b+ compile and install MySQL database from the source code
随机推荐
JUnit VS TestNG
[highly recommended] markdown grammar
Prediction of enzyme activity parameters by deep learning construction of enzyme binding model
CISSP certification 2021 textbook OSG 9th Edition added (modified) knowledge points: comparison with the 8th Edition
转载:网络加载框架 - Retrofit
洞见数据价值,启迪数字未来,《数字化的力量》问世
JUnit VS TestNG
关于 SecureFx传输远程服务器中文显示乱码 的解决方法
企业综合组网实训二
Notes de développement de la tarte aux framboises (XVI): la tarte aux framboises 4b + installe la base de données mariadb (MySQL open source Branch) et teste les opérations de base
211高校神级硕士论文刷屏!75行字错了20行!学校回应:导师停招...
SIGIR2022 | 对话式推荐系统中的用户偏好建模
CVPR2022 | 弱监督多标签分类中的损失问题
How to use metric unit buffer in PostGIS
关于 allegro的pcbEditor在使用过程中经常卡或者busy无响应 的解决方法
PLT label effective
在线文本按行批量反转工具
211高校神級碩士論文刷屏!75行字錯了20行!學校回應:導師停招...
[electronic scheme design] PCBA solution for alcohol tester
Native applet application applet - publishing process