Vue:(二十八)组件的自定义事件

38 篇文章 2 订阅

今天来学习一下自定义组件的知识,老套路,先上一波理论。

  1. 一种组件间通信的方式,适用于:自组建===>父组件

  1. 使用场景:A是父组件,B是子组件,B想给A传数据,那么就要在A中给B绑定自定义事件(事件的回调在A中)

  1. 绑定自定义事件:

  1. 第一种方式,在父组件中:<Demo @konggu="test"/>或<Demo v-on:konggu="test"/>

  1. 第二种方式,在父组件中:

<Demo ref = "demo"/>
......
mounted(){
    this.$refs.xxx.$on('konggu',this.test)
}
  1. 若想让自定义事件只能触发一次,可以使用once修饰符,或$once方法

  1. 触发自定义事件:this.$emit('konggu',数据)

  1. 解绑自定义事件:this.$off('konggu')

  1. 组件上也可以绑定原生DOM时间,需要使用native修饰符

  1. 注意:通过this.$refs.xxx.$on('konggu',回调)绑定自定义事件时,回调要么配置在methods中,要么用箭头函数,否则指向会出问题

理论完了之后就是代码环节了,因为验证的场景比较多,这里就不一一截图了,直接把所有的场景的源码贴上,需要的自取。

App.vue

<template>
  <div class="app">
    <h1>{{ msg }},学生姓名:{{ studentName }}</h1>
    <!-- 通过父组件给子组件传递函数类型的props实现:子给父传递数据 -->
    <School :getSchoolName="getSchoolName" />
    <!-- 通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第一种写法,使用@或者v-on) -->
    <!-- <Student @konggu="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',
    data() {
      return {
        msg: '你好啊',
        studentName: '',
      }
    },
    components: {
      School,
      Student,
    },
    methods: {
      getSchoolName(name) {
        console.log('App收到了学校名:', name)
      },
      getStudentName(name, ...params) {
        console.log('App收到了学生名:', name, params)
        this.studentName = name
      },
      m1() {
        console.log('App收到了m1')
      },
      show() {
        alert(123)
      },
    },
    mounted() {
      // 绑定自定义事件
      // setTimeout(() => {
      //   this.$refs.student.$on('konggu', this.getStudentName)
      // }, 3000) //等待3秒再发送
      // this.$refs.student.$once('konggu', this.getStudentName) //只触发一次
      // this.$refs.student.$on('konggu', this.getStudentName)
    },
  }
</script>
<style>
  .app {
    background-color: gray;
    padding: 5px;
  }
</style>

School.vue

<template>
  <div class="school">
    <h1>学校名称:{{name}}</h1>
    <h1>学校地址:{{address}}</h1>
    <button @click="sendSchoolName">点我发送学校名</button>
  </div>
</template>

<script>
  export default {
    name: 'SchoolVue',
    props: ['getSchoolName'],
    data() {
      return {
        name: '西航',
        address: '西安',
      }
    },
    methods: {
      sendSchoolName() {
        this.getSchoolName(this.name)
      },
    },
  }
</script>

<style>
  .school {
    background-color: blue;
    padding: 5px;
    margin-top: 30px;
  }
</style>

Student.vue

<template>
  <div class="student">
    <h1>学生姓名:{{ name }}</h1>
    <h1>年龄:{{ age }}</h1>
    <button @click="sendStudentName">点我发送学生名</button>
    <button @click="unbind">解绑konggu事件</button>
    <button @click="death">点我销毁Student实例对象</button>
  </div>
</template>

<script>
  export default {
    name: 'StudentVue',
    data() {
      return {
        name: '空谷有来人',
        age: 19,
      }
    },
    methods: {
      sendStudentName() {
        this.$emit('konggu', this.name, 555, 666, 888)
        this.$emit('demo')
      },
      unbind() {
        // this.$off('konggu') //解绑一个自定义事件
        // this.$off(['konggu', 'demo']) //解绑多个自定义事件
        this.$off() //解绑所有的自定义事件
      },
      death() {
        this.$destroy() //销毁后所有Student实例的自定义事件全都不奏效
      },
    },
  }
</script>

<style>
  .student {
    background-color: green;
    padding: 5px;
    margin-top: 30px;
  }
</style>

好了,以上就是Vue中自定义事件的定义、绑定、解绑,以及验证的场景

我是空谷有来人,谢谢支持!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值