二丫讲梵 二丫讲梵
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)

二丫讲梵

行者常至,为者常成
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)
  • Shell编程

  • Go编程笔记

  • 前端编程笔记

  • Go学习笔记

  • Vue-21年学习笔记

  • Vue-22年重学笔记

    • 基础知识

    • 脚手架工程化

      • src_分析脚手架
      • src_ref属性
      • src_props配置
      • src_mixin(混入)
      • src_插件
      • src_scoped样式
      • src_TodoList案例
      • 浏览器本地存储
      • src_TodoList_本地存储
      • src_组件自定义事件
        • 代码
          • 代码路径
          • App.vue
          • School.vue
          • Student.vue
          • main.js
        • 笔记
      • src_TodoList_自定义事件
      • src_全局事件总线
      • src_TodoList_事件总线
      • src_消息订阅与发布
      • src_TodoList_pubsub
      • src_TodoList_nextTick
      • src_过渡与动画
      • src_TodoList_动画
      • src_配置代理服务器
      • src_github搜索案例
      • 插槽
      • src_求和案例_纯Vue版本
  • 编程世界
  • Vue-22年重学笔记
  • 脚手架工程化
二丫讲梵
2022-08-17
目录

src_组件自定义事件

文章发布较早,内容可能过时,阅读注意甄别。

# 代码

# 代码路径

$ tree -N
.
├── App.vue
├── components
│   ├── School.vue
│   └── Student.vue
└── main.js
1
2
3
4
5
6
7

# App.vue

<template>
  <div class="app">
    <h1>{{ msg }} {{ studentName }}</h1>

    <!-- 通过父组件给子组件传递函数类型的props实现:子给父传递数据 -->
    <School :getSchoolName="getSchoolName" />

    <!-- 通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第一种写法,使用@或v-on) -->
    <!-- <Student @atguigu="getStudentName" @demo="m1"/> -->

    <!-- 通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第二种写法,使用ref) -->
    <Student ref="student" @click.native="show" />
  </div>
</template>

<script>
import School from "./components/School.vue";
import Student from "./components/Student.vue";
export default {
  name: "App",
  components: { School, Student },
  data() {
    return {
      msg: "你好啊!",
      studentName: "",
    };
  },
  methods: {
    getSchoolName(name) {
      console.log("收到schoolname了", name);
    },
    getStudentName(name, ...params) {
      console.log("收到studentname了", name, params);
      this.studentName = name;
    },
    m1() {
      console.log("demo事件被调用了");
    },
    show() {
      alert(123);
    },
  },
  mounted() {
    // this.$refs.student.$on('atguigu',this.getStudentName) // 绑定自定义事件
    // this.$refs.student.$once('atguigu',this.getStudentName) // 绑定自定义事件(一次性)
  },
};
</script>

<style>
.app {
  background-color: gray;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

# School.vue

<template>
  <div class="school">
    <h2>学校名称:{{ name }}</h2>
    <h2>学校地址:{{ address }}</h2>

    <!-- 通过props组件传递数据 -->
    <button @click="sendSchoolName">点我将学校名发给APP</button>
  </div>
</template>

<script>
export default {
  name: "School",
  props: ["getSchoolName"],
  data() {
    return {
      name: "shangguigua",
      address: "beijing",
    };
  },
  methods: {
    sendSchoolName() {
      this.getSchoolName(this.name);
    },
  },
};
</script>

<style scoped>
.school {
  background-color: blue;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

# Student.vue

<template>
  <div class="student">
    <h2>学生姓名:{{ name }}</h2>
    <h2>学生性别:{{ sex }}</h2>
    <h1>{{ number }}</h1>
    <button @click="add">点我给number++</button>
    <!-- 通过props组件传递数据 -->
    <button @click="sendStudentName">点我将学生名发给APP</button>
    <button @click="unbind">点我解绑自定义事件</button>
    <button @click="death">点我销毁当前Student组件的实例</button>
  </div>
</template>

<script>
export default {
  name: "Student",
  data() {
    return {
      name: "eryajf",
      sex: "男",
      number: 1,
    };
  },
  methods: {
    add() {
      console.log("add被调用了");
      this.number++;
    },
    sendStudentName() {
      // 触发student组件身上的atguigu事件
      this.$emit("atguigu", this.name, 666, 888);
      this.$emit("demo");
    },
    unbind() {
      // this.$off('atguigu') // 解绑一个自定义事件
      // this.$off(['atguigu','demo']) // 解绑多个自定义事件
      this.$off(); // 解绑所有自定义事件
    },
    death() {
      this.$destroy(); // 销毁了当前Student组件的实例,销毁后所有Student实例的自定义事件全🙅🏻不奏效
    },
  },
};
</script>

<style scoped>
.student {
  background-color: orange;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

# main.js

import Vue from "vue";
import App from "./App.vue";

Vue.config.productionTip = false;

new Vue({
  el: "#app",
  components: { App },
  render: (h) => h(App),
});
1
2
3
4
5
6
7
8
9
10

# 笔记

  1. 一种组件间通信的方式,适用于:子组件 ===> 父组件
  2. 使用场景:A 是父组件,B 是子组件,B 想给 A 传数据,那么就要在 A 中给 B 绑定自定义事件(事件的回调在 A 中)。
  3. 绑定自定义事件:
  4. 第一种方式,在父组件中:<Demo @atguigu="test"/> 或 <Demo v-on: atguigu="test"/>
  5. 第二种方式,在父组件中:
    <Demo ref="demo"/>
    ......
    mounted(){
       this.$refs.xxx.$on('atguigu',this.test)
    }
    
    1
    2
    3
    4
    5
  6. 若想让自定义事件只能触发一次,可以使用 once 修饰符,或 $once 方法。
  7. 触发自定义事件:this.$emit('atguigu',数据)
  8. 解绑自定义事件 this.$off('atguigu')
  9. 组件上也可以绑定原生 DOM 事件,需要使用 native 修饰符。
  10. 注意:通过 this.$refs.xxx.$on('atguigu',回调) 绑定自定义事件时,回调要么配置在 methods 中,要么用箭头函数,否则 this 指向会出问题!
微信 支付宝
上次更新: 2024/06/13, 22:13:45
src_TodoList_本地存储
src_TodoList_自定义事件

← src_TodoList_本地存储 src_TodoList_自定义事件→

最近更新
01
学习周刊-总第212期-2025年第21周
05-22
02
从赵心童世锦赛夺冠聊聊我的斯诺克情缘
05-16
03
学习周刊-总第211期-2025年第20周
05-15
更多文章>
Theme by Vdoing | Copyright © 2017-2025 | 点击查看十年之约 | 浙ICP备18057030号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式