小于博客 小于博客
首页
  • Java学习笔记
  • Docker专区
  • 实战教程
  • Shell
  • 内存数据库
  • Vue学习笔记
  • Nginx
  • Php
  • CentOS
  • Docker
  • Gitlab
  • GitHub
  • MySql
  • MongoDB
  • OpenVPN
  • 配置文件详解
  • Other
  • ELK
  • K8S
  • Nexus
  • Jenkins
  • 随写编年
  • 电影音乐
  • 效率工具
  • 博客相关
  • 最佳实践
  • 迎刃而解
  • 学习周刊
关于
友链
  • 本站索引

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

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • 网站状态 (opens new window)
    • json2go (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)

小于博客

行者常至,为者常成
首页
  • Java学习笔记
  • Docker专区
  • 实战教程
  • Shell
  • 内存数据库
  • Vue学习笔记
  • Nginx
  • Php
  • CentOS
  • Docker
  • Gitlab
  • GitHub
  • MySql
  • MongoDB
  • OpenVPN
  • 配置文件详解
  • Other
  • ELK
  • K8S
  • Nexus
  • Jenkins
  • 随写编年
  • 电影音乐
  • 效率工具
  • 博客相关
  • 最佳实践
  • 迎刃而解
  • 学习周刊
关于
友链
  • 本站索引

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

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • 网站状态 (opens new window)
    • json2go (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)
  • Java学习笔记

  • Docker专区

  • Shell编程

  • 实战教程

  • 内存数据库

  • Vue学习笔记

    • 基础知识

    • 脚手架工程化

      • 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学习笔记
  • 脚手架工程化
小于博客
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. 第二种方式,在父组件中:
  ```js
  <Demo ref="demo"/>
  ......
  mounted(){
     this.$refs.xxx.$on('atguigu',this.test)
  }
  ```
  1. 若想让自定义事件只能触发一次,可以使用 once 修饰符,或 $once 方法。
  2. 触发自定义事件:this.$emit('atguigu',数据)
  3. 解绑自定义事件 this.$off('atguigu')
  4. 组件上也可以绑定原生 DOM 事件,需要使用 native 修饰符。
  5. 注意:通过 this.$refs.xxx.$on('atguigu',回调) 绑定自定义事件时,回调要么配置在 methods 中,要么用箭头函数,否则 this 指向会出问题!
上次更新: 2024/02/28, 13:00:35

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

最近更新
01
SpringBoot 快速实现 api 加密!
03-21
02
SpringBoot整合SQLite
03-07
03
SpringBoot配置使用H2数据库的简单教程
02-21
更多文章>
Theme by Vdoing | Copyright © 2017-2024 | 点击查看十年之约 | 豫ICP备2022014539号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式