当前位置:网站首页>我们说的组件自定义事件到底是什么?
我们说的组件自定义事件到底是什么?
2022-07-24 08:55:00 【刘家奕_】
顾名思义,是用于组件之间的自定义事件,下面我将用3个例子去说明(父子组件的通信问题)

student.vue代码
<template>
<div class="student">
<h2>学生姓名:{
{ studentName }}</h2>
<h2>学生年龄:{
{ studentAge }}</h2>
<button @click="sendStudentName">把学生的姓名给App</button>
</div>
</template>
<script>
export default {
name: "Student",
props: ["getStudentName"],
data() {
return {
studentName: "刘家奕",
studentAge: 19,
};
},
methods: {
sendStudentName() {
this.getStudentName(this.studentName);
},
},
};
</script>
<style>
.student {
background-color: lightblue;
padding: 5px;
}
</style>
app.vue的代码为
<template>
<div class="app">
<h2>{
{ msg }}</h2>
<!-- /第一种方法: 通过props -->
<Student :getStudentName="getStudentName" />
<!-- 第二种方法: 自定义组件 简写形式为 <School @liujiayi="getSchoolName" /> -->
<!-- <School v-on:liujiayi="getSchoolName" /> -->
<!-- 第三种方法:ref实现 -->
<School ref="school" />
</div>
</template>
<script>
import Student from "./components/Student";
import School from "./components/School";
// import { computed } from "@vue/runtime-core";
export default {
name: "App",
components: {
Student,
School,
},
data() {
return {
msg: "你好呀",
};
},
methods: {
getStudentName(name) {
console.log("app得到了学生的名字为", name);
},
getSchoolName(name) {
console.log("app得到了学校的名字为", name);
},
},
mounted() {
this.$refs.school.$on("liujiayi", this.getSchoolName);
// $on是当什么时候的意思,在此处是当自定义组件被触发时
},
};
</script>
<style>
.app {
background-color: gray;
padding: 5px;
}
</style>
school.vue代码为:
<template>
<div class="school">
<h2>学校名称:{
{ schoolName }}</h2>
<h2>学校地址:{
{ address }}</h2>
<button @click="sendSchoolName">把学校的名字给App</button>
</div>
</template>
<script>
// import { emit } from "process";
export default {
name: "School",
data() {
return {
schoolName: "南昌航空大学",
address: "江西",
};
},
methods: {
sendSchoolName() {
this.$emit("liujiayi", this.schoolName);
},
},
};
</script>
<style>
.school {
background-color: yellow;
padding: 10px;
margin-top: 30px;
}
</style>
3种方法在app.vue里有注释
网页效果:

边栏推荐
- 2、 Encapsulation and tool classes for adding, deleting, modifying and checking in midway
- 林业调查巡检数据采集解决方案
- Typora提示【This beta version of Typora is expired, please download and install a newer version】的解决方案
- 剑指 Offer II 024. 反转链表
- Unity C#工具类 ArrayHelper
- How to integrate and use log4net logging plug-in in vs2019 class library
- Group by group and get the first data
- mysql SUBSTRING_ Application of index in business scenarios
- 3、 Midway interface security certification
- Xtrabackup realizes full backup and incremental backup of MySQL
猜你喜欢

面试官:哥们Go语言的读写锁了解多少?

Houdini 官方HDA SideFX Labs 安装

On express framework
![[Sheung Shui Shuo series] EE feedback details](/img/19/5a80d5ef80d75be74b7ce08d6df058.png)
[Sheung Shui Shuo series] EE feedback details

Treap

Taking advantage of the momentum, oceanbase promotes the lean growth of digital payment

Meta tags let search engines search your website

Take out the string in brackets

链表——24. 两两交换链表中的节点

Leetcode94-二叉树的中序遍历详解
随机推荐
阻塞队列BlockingQueue 源码解析(ArrayBQ和LinkedBQ)
Using golang to implement RPC (1)
Take out the string in brackets
[Shangshui Shuo series together] June summary +no anxiety +july plan + how to test + how to improve
How should tiktok account operate?
C语言练习题目+答案:
Group by group and get the first data
After two days of tossing and turning, I finally wrote my first fluent app, which almost tortured me into a nervous breakdown
1、 Midwey addition, deletion, modification and query
3587. Connected graph (Jilin University postgraduate entrance examination machine test question)
0 threshold takes you to know two-point search
Ubuntu20.04 install MySQL 5.7
链表——19. 删除链表的倒数第 N 个结点
Leetcode94-二叉树的中序遍历详解
Realize page return to parent directory based on cookies
C language practice questions + Answers:
Guys, what parameters can be set when printing flinksql so that the values can be printed? This later section is omitted. It's inconvenient. I read the configuration on the official website
[Sheung Shui Shuo series] EE feedback details
How to integrate and use log4net logging plug-in in vs2019 class library
Protocol buffer learning notes