Vue中设置组件的自定义事件和解绑组件的自定义事件

组件的自定义事件

  • 设置School组件实现props,子给父传数据。School组件用于和Student组件做对比。

  • App向School组件传getSchoolName,School组件通过props接收存入School的VueConponent。随后通过调用getSchoolName实现子给父传递数据

  • 想让组件标签识别原生Vue提供的事件。需要加入native

    <School @click.native='show'/>
  • <template>
      <div class="school">
       <h2>学校的名字:{
        { name  }}</h2>
       <button @click="sendSchoolName">测试sendSchoolName方法</button>
      </div>
    </template>
    ​
    <script>
    export default {
      props: ['getSchoolName'],
        data(){
            return {
             name: '小猴子的玩具商'
            }
        },
        methods:{
          sendSchoolName(){
            this.getSchoolName(this.name)
          }
        },
    }
    </script>
    <style scoped>
      .school {
        background-color: pink;
        padding: 5px;
        margin-top: 30px;
      }
    </style>>
    ​
  • Student组件实现功能也是子给父传递数据。但用组件的自定义事件来实现

  • <template>
      <div class="blue">
        <h2>学生的名字:{
        { name }}</h2>
        <h2>学生的名字:{
        { address }}</h2>
       <button @click="sendStudentName">测试sendStudentName方法</button>
      </div>
    </template>
    <script>
    export default {
        name:'Student',
        data(){
            return {
                name:'boy',
                address:'老地方'
            }
        },
        methods: {
            sendStudentName(){
                // 事件在谁身上则在哪里触发,故触发Student组件实例身上的boy事件,emit有发射的意思。传了一个参数name给App
                this.$emit('boy',this.name)
            }
        },
    }
    </script>
    <style scoped>
        .blue{
            background-color: blue;
            padding: 5px;
        }
    </style>
  • Student和School组件实现的区别在于

    1、App中的getSchoolName方法是直接给School组件,School组件需要props接收一下,自己调用一下。

    2、App中的getStudentName方法不直接传给Student组件,而是利用触发事件( this.$emit() ) 的方式,调用demo

  • <template>
      <div class="title">
        <router-view/>
        <h2>{
        { mas }}</h2>
        <!-- 给组件的实列对象VueComponent上绑定事件(boy事件)。如果触发boy事件demo就被调用 -->
        <!-- 第一种写法,<Student v-on:boy = "demo"/> -->
        <!-- 第二种写法,用ref获取Student组件的VueComponent-->
          <Student ref="student"/>
          
        <!-- 通过父组件给子组件传递函数类型的props实现:子给父传递数据 -->
        <School :getSchoolName ='getSchoolName'/>
      </div>
    </template>
    ​
    <script>
    import Student from '../src/components/Student.vue'
    import School from '../src/components/School.vue'
    export default {
      name: 'App',
      components:{Student,School},
      data(){
        return {
          mas: '你好啊'
        }
      },
      methods: {
          //接收School组件传过来的name
        getSchoolName(name){
          console.log('发送成功了',name);
        },
        //接收Student组件传过来的name
        demo(name){
          console.log('demo被调用',name)
        }
      }
    },
     // 第二种写法的灵活性更高,当要求是:延迟3秒才触发事件。第二种方法可以实现。如下
      mounted(){
        setTimeout(()=>{
          // this.$refs.student为获取到Student组件的VueComponent,$on为当什么的时候。括号内的第一个元素为事件名,第二个元素为触发事件回调的方法
        this.$refs.student.$on('boy', this.demo)
        },3000)
      }
    </script>
    <style scoped>
       .title {
        background-color: gray;
        padding: 5px;
       }
     

    解绑组件的自定义事件

    • this.$off()//解绑一个自定义事件

    • this.$off([ ])//解绑多个自定义事件

    • this.$off()//解绑所有的自定义事件

    • this.$destroy()销毁当前组件的VueConponent,而Vue提供的事件还能响应

    • <template>
        <div class="blue">
          <h2>学生的名字:{
            { name }}</h2>
          <h2>学生的名字:{
            { address }}</h2>
         <button @click="sendStudentName">测试sendStudentName方法</button>
         <button @click="unbind">解绑自定义事件</button>
         <button @click="death">销毁当前组件的实例化对象</button>
      ​
        </div>
      </template>
      ​
      <script>
      export default {
          name:'Student',
          data(){
              return {
                  name:'boy',
                  address:'老地方'
              }
          },
          methods: {
              sendStudentName(){
                  // 事件在谁身上则在哪里触发,故触发Student组件实例身上的boy事件,emit有发射的意思。传了一个参数name
                  this.$emit('boy',this.name)
                  this.$emit('girl')
              },
              unbind(){
                  this.$off('boy')//解绑一个自定义事件
                  this.$off(['boy','girl'])//解绑多个自定义事件
                  this.$off()//解绑所有的自定义事件
              },
              death(){
                  //销毁当前的Student组件的实例,销毁后所有的Student实列的自定义事件全都不奏效。
                  this.$destroy()
              }
          },
      }
      </script>
      <style scoped>
          .blue{
              background-color: blue;
              padding: 5px;
          }
      </style>

相关推荐

  1. Vue定义事件组件间通讯艺术

    2023-12-30 21:50:06       21 阅读
  2. vue 定义事件组件方法调用

    2023-12-30 21:50:06       29 阅读
  3. Vue2:用ref方式定义事件注意事项

    2023-12-30 21:50:06       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-30 21:50:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-30 21:50:06       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-30 21:50:06       82 阅读
  4. Python语言-面向对象

    2023-12-30 21:50:06       91 阅读

热门阅读

  1. 51单片机三种编译模式的相互关系

    2023-12-30 21:50:06       49 阅读
  2. [spark] 存储到hdfs时指定分区

    2023-12-30 21:50:06       59 阅读
  3. SSH连接docker容器

    2023-12-30 21:50:06       54 阅读
  4. oj 1.8编程基础之多维数组 13:图像模糊处理

    2023-12-30 21:50:06       57 阅读
  5. 单片机的最小系统

    2023-12-30 21:50:06       53 阅读
  6. Python函数中的*args,**kwargs作用与用法

    2023-12-30 21:50:06       59 阅读
  7. Jmeter学习总结(6)——Beanshell中If和For应用

    2023-12-30 21:50:06       58 阅读
  8. 编程笔记 html5&css&js 014 网页布局框架

    2023-12-30 21:50:06       60 阅读
  9. C++哈希表(unordered_map和unordered_set)

    2023-12-30 21:50:06       44 阅读
  10. ✨ 2024新年Flag ✨

    2023-12-30 21:50:06       46 阅读