当前位置:网站首页>Custom events in components
Custom events in components
2022-07-23 10:45:00 【Dignified 304】
Custom events for components
1. A way of communicating between components , Apply to :<strong style="color:red"> Child components ===> Parent component </strong>
2. Use scenarios :A It's the parent component ,B It's a subcomponent ,B Want to give A The data transfer , Then it's in A Middle feeding B Binding custom events (<span style="color:red"> The callback of the event is in A in </span>).
3. Binding custom events :
1. The first way , In parent component :```<Demo @atguigu="test"/>``` or ```<Demo v-on:atguigu="test"/>```
2. The second way , In parent component :
```js
<Demo ref="demo"/>
......
mounted(){
this.$refs.xxx.$on('atguigu',this.test)
}
```
3. If you want a custom event to be triggered only once , have access to ```once``` Modifier , or ```$once``` Method .
4. Trigger custom event :```this.$emit('atguigu', data )```
5. Unbind custom events ```this.$off('atguigu')```
6. Components can also be bound to native DOM event , Need to use ```native``` Modifier .
7. Be careful : adopt ```this.$refs.xxx.$on('atguigu', Callback )``` When Binding custom events , Callback <span style="color:red"> Or configure it in methods in </span>,<span style="color:red"> Or use the arrow function </span>, otherwise this There will be problems !
design sketch

Parent component generation
<div>
<div class="demo">
<h3> The name of the school {
{ name }}</h3>
<h3> School address {
{ address }}</h3>
<h4 v-show="stuName"> Student name received {
{ stuName }}</h4>
</div>
<!-- @getStuName Is a custom time name -->
<Student @getStuName="getStuName"></Student>
</div>
<script>
import Student from "./Student.vue";
export default {
name: "School",
components: { Student },
data() {
return {
name: "atguigu",
address: " Beijing ",
stuName: "",
};
},
methods: {
getStuName(name) {
console.log(" I am a School", name);
this.stuName = name;
},
},
};
</script>
Subcomponent code
<div class="demo">
<h1> The student's name {
{ name }}</h1>
<button @click="send(name)"> Click on </button>
</div><script>
export default {
name: "Student",
data() {
return {
name: " Zhang San ",
};
},
methods: {
send(name) {
//$emit Call custom events
this.$emit("getStuName", name);
},
},
// Unbind custom events
beforeDestroy() {
this.$off("getStuName");
},
};
</script>The second method
utilize ref choice
<div>
<div class="demo">
<h3> The name of the school {
{ name }}</h3>
<h3> School address {
{ address }}</h3>
<h4 v-show="stuName"> Student name received {
{ stuName }}</h4>
</div>
<!-- @getStuName Is a custom time name -->
<Student ref="Stu"></Student>
</div>import Student from "./Student.vue";
export default {
name: "School",
components: { Student },
data() {
return {
name: "atguigu",
address: " Beijing ",
stuName: "",
};
},
methods: {
getStuName(name) {
console.log(" I am a School", name);
this.stuName = name;
},
},
mounted() {
//$on The binding event
this.$refs.Stu.$on("getStuName", this.getStuName);
},
};
</script>边栏推荐
- More detailed series than your teacher -- structure
- openvino_datawhale
- PowerBI入门指南
- SAP batch import template (WBS batch import as an example)
- Basic process of dpdk cross compilation
- 【Delphi】制作控件面板安装图标的简单方法(译)
- What is instant messaging? Development of instant messaging
- 04_ UE4 advanced_ Introduction to physical collision and firing fireballs
- 300 题 第六讲 二次型
- Chapter 3 Standard Input
猜你喜欢

How to protect the copyright of NFT digital collections?

Chapter 4: runtime data area - shared space

PXE远程安装和Kickstart无人值守安装 技术文档

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

How to delete an item in sonar

Exciting metauniverse! Wealth outlet of next generation Internet

Self operation and maintenance: a new sample of it operation and maintenance services in Colleges and Universities

【Unity日常Bug】Unity报错Unexpected character ‘‘

Clion + mingw64 configure C language development environment visual studio installation

Response object
随机推荐
理解ASP.NET Core - 基于Cookie的身份认证(Authentication)
openvino_datawhale
2022/7/20
C# 客户端程序调用外部程序的3种实现方法
Exciting metauniverse! Wealth outlet of next generation Internet
mysql log理解
Flask学习笔记
Flask learning notes
Response object
How to delete an item in sonar
Leetcode skimming -- bit by bit record 022
分期付款中的利率问题
Add trust list
SQLZOO——SELECT from WORLD Tutorial
MySQL master-slave replication
【Unity日常Bug】Unity报错Unexpected character ‘‘
What is the core essence of smart parks?
CV (3)- CNNs
CLion + MinGW64配置C语言开发环境 Visual Studio安装
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