当前位置:网站首页>我们说的组件自定义事件到底是什么?
我们说的组件自定义事件到底是什么?
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里有注释
网页效果:

边栏推荐
- Hack the box - File Inclusion module detailed Chinese tutorial
- Tiktok shop will add a new site, and the Singapore site will be launched on June 9
- Route interceptor
- C language practice questions + Answers:
- First acquaintance with JVM
- Relationship between fork and pipeline
- Grpc learning notes
- 使用Go语言开发eBPF程序
- Discuz论坛搭建详细过程,一看就懂
- Virtual machine terminator terminal terminator installation tutorial
猜你喜欢

阻塞队列BlockingQueue 源码解析(ArrayBQ和LinkedBQ)

Houdini official HDA sidefx labs installation

Wargames NATAS (0-10) problem solving essay

xtrabackup 实现mysql的全量备份与增量备份

3587. Connected graph (Jilin University postgraduate entrance examination machine test question)

How to configure env.js in multiple environments in uni app

Xtrabackup realizes full backup and incremental backup of MySQL

Treap

脉脉网友出了道 Go 面试题,你能答对吗?

Hack the box - File Inclusion module detailed Chinese tutorial
随机推荐
Confusing defer, return, named return value, anonymous return value in golang
5、 Use of environment variables in midway
看了纪录片《埃达克岛的海盗宝藏》,想到我国历史上的遗失秘宝
Xiaobai learns Jenkins - installation and quick start
[Shangshui Shuo series] final rad New Literacies
Recursive performance test
C语言练习题目+答案:
VGA character display based on FPGA
Tiktok video traffic golden release time
Typescript -- Generic
利用opencv 做一个简单的人脸识别
C# 简述里氏替换原则的应用
Using OpenCV to do a simple face recognition
[translation] integration challenges in microservice architecture using grpc and rest
What is the "age limit" on tiktok and how to solve it?
Data collection solution for forestry survey and patrol inspection
Super complete summary: how to operate files in go language
Treap
Configuration of uni app page.json title bar
Sword finger offer II 024. reverse linked list