当前位置:网站首页>组件中的自定义事件
组件中的自定义事件
2022-07-23 04:32:00 【道貌岸然304】
组件的自定义事件
1. 一种组件间通信的方式,适用于:<strong style="color:red">子组件 ===> 父组件</strong>
2. 使用场景:A是父组件,B是子组件,B想给A传数据,那么就要在A中给B绑定自定义事件(<span style="color:red">事件的回调在A中</span>)。
3. 绑定自定义事件:
1. 第一种方式,在父组件中:```<Demo @atguigu="test"/>``` 或 ```<Demo v-on:atguigu="test"/>```
2. 第二种方式,在父组件中:
```js
<Demo ref="demo"/>
......
mounted(){
this.$refs.xxx.$on('atguigu',this.test)
}
```
3. 若想让自定义事件只能触发一次,可以使用```once```修饰符,或```$once```方法。
4. 触发自定义事件:```this.$emit('atguigu',数据)```
5. 解绑自定义事件```this.$off('atguigu')```
6. 组件上也可以绑定原生DOM事件,需要使用```native```修饰符。
7. 注意:通过```this.$refs.xxx.$on('atguigu',回调)```绑定自定义事件时,回调<span style="color:red">要么配置在methods中</span>,<span style="color:red">要么用箭头函数</span>,否则this指向会出问题!
效果图

父组件代
<div>
<div class="demo">
<h3>学校名字{
{ name }}</h3>
<h3>学校地址{
{ address }}</h3>
<h4 v-show="stuName">学生姓名已收到{
{ stuName }}</h4>
</div>
<!-- @getStuName是自定义时间名 -->
<Student @getStuName="getStuName"></Student>
</div>
<script>
import Student from "./Student.vue";
export default {
name: "School",
components: { Student },
data() {
return {
name: "atguigu",
address: "北京",
stuName: "",
};
},
methods: {
getStuName(name) {
console.log("我是School", name);
this.stuName = name;
},
},
};
</script>
子组件代码
<div class="demo">
<h1>学生姓名{
{ name }}</h1>
<button @click="send(name)">点击</button>
</div><script>
export default {
name: "Student",
data() {
return {
name: "张三",
};
},
methods: {
send(name) {
//$emit调用自定义事件
this.$emit("getStuName", name);
},
},
// 解绑自定义事件
beforeDestroy() {
this.$off("getStuName");
},
};
</script>第二种方法
利用ref选择
<div>
<div class="demo">
<h3>学校名字{
{ name }}</h3>
<h3>学校地址{
{ address }}</h3>
<h4 v-show="stuName">学生姓名已收到{
{ stuName }}</h4>
</div>
<!-- @getStuName是自定义时间名 -->
<Student ref="Stu"></Student>
</div>import Student from "./Student.vue";
export default {
name: "School",
components: { Student },
data() {
return {
name: "atguigu",
address: "北京",
stuName: "",
};
},
methods: {
getStuName(name) {
console.log("我是School", name);
this.stuName = name;
},
},
mounted() {
//$on绑定事件
this.$refs.Stu.$on("getStuName", this.getStuName);
},
};
</script>边栏推荐
- Toco generates tflite model
- Sequence model (2) - natural language processing and word nesting
- SVG、canvas、绘制线段和填充多边形、矩形、曲线的绘制和填充
- Add trust list
- 数据湖:Apache Iceberg介绍
- What is the core essence of smart parks?
- 常见神经网络参数量与计算量
- redis 复制集群搭建
- 为什么我们无法写出真正可重用的C#/F#代码?
- 7. < tag dynamic programming and stock trading Collection> lt.121. The best time to buy and sell stocks + lt.122. The best time to buy and sell stocks II + lt.123. The best time to buy and sell stocks
猜你喜欢

Exciting metauniverse! Wealth outlet of next generation Internet

数据湖:Apache Iceberg介绍

Kingbasees SQL language reference manual of Jincang database (8. Function (7))

Response对象

Customer first | domestic Bi leader, smart software completes round C financing

2022/7/21

kex_exchange_identification: read: Connection reset by peer 不完美解决办法(之一)

How to delete an item in sonar

IDEA 集成 Sonar 完整流程

TZC 1283: simple sort - heap sort
随机推荐
推荐一款 Shell 装逼神器,已开源!网友:真香。。。
How does VirtualBox set up port forwarding?
Ultra Fast Deep Lane Detection with Hybrid Anchor Driven Ordinal Classification论文解读
注册树模式
2022/7/20
Kingbasees SQL language reference manual of Jincang database (8. Function (III))
7. < tag dynamic programming and stock trading Collection> lt.121. The best time to buy and sell stocks + lt.122. The best time to buy and sell stocks II + lt.123. The best time to buy and sell stocks
序列模型(二)- 自然语言处理与词嵌套
selenium JD爬虫
NFT数字藏品版权如何保护?
比你老师详细系列————结构体
Self operation and maintenance: a new sample of it operation and maintenance services in Colleges and Universities
How to protect the copyright of NFT digital collections?
Richview textbox items textbox
7.< tag-动态规划和买卖股票合集>lt.121. 买卖股票的最佳时机 + lt.122.买卖股票的最佳时机 II+ lt.123. 买卖股票的最佳时机 III dbc
How Alibaba cloud resolves a domain name to another domain name
MySQL主从复制
Unityc realizes the conversion of Chinese characters to Pinyin - using Microsoft chspinyinconv Library
分期付款中的利率问题
JMeter record the BeanShell written into excel instance caused by an automatic data generation