当前位置:网站首页>组件传值:子组件向父组件传递数据
组件传值:子组件向父组件传递数据
2022-06-21 22:05:00 【Nancy-sn】
子组件通过$emit方法用来触发事件
1.子组件定义一个方法,点击后会发出send
2.send被触发后,将触发parentsend,携带数据
3.自定义的事件,绑定父组件的方法handler()
4.调用父组件的方法
<!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">
<p>{
{parent_msg}}</p>
<child @parentsend="handler"></child>
</div>
</body>
</html>
<script src="./js/vue.js"></script>
<script>
//全局组件(子组件)
Vue.component("child",{
template:`
<div>
<button @click=send>发送给父组件</button>
</div>
`,
data(){
return{
child_msg:"子组件的数据"
}
},
methods:{
send(){
//触发了parentsend,在parentsend携带child_msg
this.$emit("parentsend",this.child_msg)
}
},
})
//父组件
new Vue({
el:"#app",
data:{
parent_msg:""//父组件的data
},
methods:{
handler(msg){
this.parent_msg=msg;
}
}
})
</script>运行后:

点击按钮后:
边栏推荐
- Rk3568 Development Notes (II): introduction to the kit, backplane and peripheral test of rk3568 development board
- Enterprise wechat built-in application H5 development record-1
- Uni app advanced style framework / production environment [Day10]
- SIGIR2022 | 對話式推薦系統中的用戶偏好建模
- 盘点常见的漏洞利用方式
- Solutions to the problem that Allegro's pcbeditor is often stuck or busy in use
- windows sql server 如何卸载干净?
- 211 college Master of divinity thesis swipe the screen! 75 lines wrong 20 lines! School response: the tutor stopped recruiting
- WSL 2 installation process (and introduction)
- 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
猜你喜欢
![[数据库课程设计]基于Sql Server的教室信息管理系统(附部分源码)](/img/7e/47011ee2a35c50669a86fd5cf543d9.png)
[数据库课程设计]基于Sql Server的教室信息管理系统(附部分源码)

Prediction of enzyme activity parameters by deep learning construction of enzyme binding model

QT document reading notes staticmetaobject parsing and instances

You have a chance, here is a stage

Software testing Q & A
Uni app advanced style framework / production environment [Day10]

树莓派开发笔记(十五):树莓派4B+从源码编译安装mysql数据库

Win11热点连接成功但没网?Win11移动热点和网络冲突的解决方法
![[highly recommended] markdown grammar](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[highly recommended] markdown grammar
![Detailed explanation of C language [implicit type conversion] and [explicit type conversion]](/img/95/5acd2dd7b800b43435b52a70001a74.png)
Detailed explanation of C language [implicit type conversion] and [explicit type conversion]
随机推荐
Matplotlib drawing and displaying Chinese
Unity-网络开发(二)
C语言【隐式类型转换】和【显式类型转换】的详解
上海交通大学:Kerong Wang | 基于离线强化学习的自举Transformer
Analysis of Eureka
Hardware development notes (IV): basic process of hardware development, making a USB to RS232 module (III): design schematic diagram
redis主从复制(九)
windows sql server 如何卸载干净?
Software testing concepts
企业综合组网实训二
IPD chip shipments exceeded 1billion, and chips and semiconductors appeared in ims2022
SIGIR2022 | 對話式推薦系統中的用戶偏好建模
Sigir2022 | modélisation des préférences des utilisateurs dans le système de recommandation dialogique
有一说一,高并发系统设计其实一点都不难!
Produced by Ali! Graphical ant script - idea plug-in cloudtoolkit
Truncate strings by length into an array
Ah ah ah ah ah ah
洞見數據價值,啟迪數字未來,《數字化的力量》問世
Inventory common vulnerability utilization methods
Unity-网络开发(一)