美文网首页
10-内置事件和组件自定义事件

10-内置事件和组件自定义事件

作者: 荆承鹏 | 来源:发表于2022-07-18 16:33 被阅读0次

1. 介绍

@click, keyup, keydown, update 等这些都是 js 内置的事件,可以直接用。
自定义事件是给组件用的。

2. 子向父组件传值的方法

<!-- 通过父组件给子组件传递函数类型的 props实现:子给父传递数据 -->
<School :getSchoolName="getSchoolName"/>
<!-- 通过父组件给子组件绑定一个自定义组件事件实现:子给父传递数据 (第一种写法:通过@或者v-on绑定事件)-->
<!-- 自定义了一个事件atguigu,这个事件被绑定在了组件 Student 的实例对象 vc上,所以当这个组件被触发的时候,调用函数sendStudentName -->
<!-- <Student v-on:atguigu="getStudentName"/> -->

<!-- 通过父组件给子组件绑定一个自定义组件事件实现:子给父传递数据 (第二种写法:通过ref绑定事件)-->
<!-- 通过this.$refs.student 可以拿到Student实例对象vc -->
<Student ref="student"/> 

3. 代码

父级组件 app

<template>
  <div>
      <div id="mainContent">
        <h1>自定义组件事件</h1>
        <!-- 通过父组件给子组件传递函数类型的 props实现:子给父传递数据 -->
          <School :getSchoolName="getSchoolName"/>
          <!-- 通过父组件给子组件绑定一个自定义组件事件实现:子给父传递数据 (第一种写法:通过@或者v-on绑定事件)-->
          <!-- 自定义了一个事件atguigu,这个事件被绑定在了组件 Student 的实例对象 vc上,所以当这个组件被触发的时候,调用函数sendStudentName -->
          <!-- <Student v-on:atguigu="getStudentName"/> -->

          <!-- 通过父组件给子组件绑定一个自定义组件事件实现:子给父传递数据 (第二种写法:通过ref绑定事件)-->
          <!-- 通过this.$refs.student 可以拿到Student实例对象vc -->
          <Student ref="student"/> 
      </div>
  </div>
</template>

<script>
//import 引入所有组件
import Student from './components/Student.vue'
import School from './components/School.vue'

export default {
    name: "App", // 给app组建命名为 App
    //注册注册组件 components: { School,Student },
    components: {School,Student},
    data(){
      return {
      }

    },
    methods:{
      getSchoolName(name){
        console.log("我收到了学校的名字", name)
      },
      getStudentName(name){
        console.log("自定义组件事件被调用了,我收到了数据", name)
      }
    },
    mounted(){
      //在mounted挂载完毕以后,执行下面的操作
      //this.$refs.student 拿到了Student组件的实例对象vc,$on 是给vc绑定事件。时间名为 atguigu, 事件回调函数为 this.getStudentName
      this.$refs.student.$on("atguigu", this.getStudentName)
    }

}
</script>
<style>

</style>

子组件 School.vue

<template>
  <div id="school">
    学校名:{{msg2}}
    <br><br>
    学校地址:{{address}}
    <br><br>
    <button @click="sendSchoolName">点击我把学校名称传到 app</button>
  </div>
</template>

<script>
export default {
    name:"School",
    props:["getSchoolName"],
    data(){
        return {
            msg2: "jing c p",
            address:"长安区"
        }
    },
    methods:{
        sendSchoolName(){
            this.getSchoolName(this.msg2)
        }
    }

}
</script>

<style scoped>
#school{
    background-color: grey;
    height: 200px;
}
</style>

子组件 Student.vue

<template>
  <div id="student">
    学生名:{{msg}}
    <br><br>
    学生年龄:{{age}}
    <br><br>
    <button @click="sendStudentName">点击把学生名字传到 app</button>
  </div>
</template>

<script>

export default {
    name: "Student",
    data(){
        return {
            msg: "Jackie",
            age:18
        }
    },
    methods:{
        sendStudentName(){
        this.$emit("atguigu", this.msg)
    }
    }
}
</script>

<style scoped>
#student{
    background-color: green;
    height: 200px;
}
</style>

4.解绑自定义组件事件

在哪个组件上绑定的事件,就在哪个组件上解绑。解绑的方式有四种

methods:{
unbind(){
this.$off("事件名") //解绑一个事件
this.$off() // 解绑所有事件
this.$off(["事件名1","事件名2"]) //解绑两个事件或更多
//还有一种方法是把整个组件的实例对象销毁,组件(vc)销毁以后在组件上的自定义事件也被销毁了
}
}

5. 总结

  1. 自定义组件事件是一种组件间通信的方式,适用于:子给父组件传递信息。

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

  3. 绑定自定义事件
    第一种方式:
    在父组件中:<Dome @aiguigu="test"/> 或者<Dome v-on:atguigu="test"/>

第二种方式:
在父组件中:
<Demo ref="demo"/>
....
mounted(){ this.$refs.xxx.$on("atguigu", this.test) }

备注:如果想让自定义事件只触发一次,可以使用 once修饰符或者$once方法

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

  2. 解绑自定义事件:this.$off('atguigu')

  3. 组件上也可以绑定原生dom事件,如果click事件,但是为了让其生效需要使用修饰符native, 比如 <Demo @click.native="test"/>

  4. 注意:通过this.$refs.xxx.$on('atguigu', 回调函数) 绑定自定义事件的,回调要么配置在methods里,要么使用箭头函数,否则this指向会有问题。

相关文章

  • 10-内置事件和组件自定义事件

    1. 介绍 @click, keyup, keydown, update 等这些都是 js 内置的事件,可以直接用...

  • vue组件的使用

    props和$emit 组件间通讯和自定义事件 父子通讯 props $emit兄弟组件或者隔代组件使用自定义事件...

  • 2019-01-31 vue组件基础篇2

    子组件 ═══>向父组件传递数据时,就要用到自定义事件v-on除了监听DOM事件外,还可以用于组件之间的自定义事件...

  • .native && $listeners

    将原生事件绑定到自定义组件 原生事件在自定义组件上是不起作用的 添加修饰符.native,原生事件在自定义组件上就...

  • element ui的el-input如何监听回车键

    @keyup.enter.native 组件没有内置keyup事件,所以给组件绑定原生事件 需要加上.native...

  • Lightning Web Component 事件代码示例

    组件的事件 组件通过各种事件来进行通讯。 在 LWC 中,可以通过实现 CustomEvent 接口进行自定义事件...

  • Vue.js 自定义事件

    自定义事件 当子组件需要向父组件传递数据时,就要用到自定义事件。指令 v-on 除了监听 DOM 事件外,还可以用...

  • $emit()

    vue 为每个组件对象提供了一个内置方法 $emit ,它等同于自定义事件中的 new Event,trigger...

  • #React中的事件

    这里的事件:React内置的DOM组件中的事件 1.给document注册事件 2.几乎所有的元素的事件处理,均在...

  • $emit自定义事件

    $emit自定义事件,常常用于子组件向父组件通信定义事件:this.$emit(事件名称,参数1,参数2……参数n...

网友评论

      本文标题:10-内置事件和组件自定义事件

      本文链接:https://www.haomeiwen.com/subject/bnldirtx.html