欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

解绑组件自定义事件

1
2
3
4
5
6
7
// this.$off('defineMyEvent')//解绑一个自定义事件
 
//解绑多个自定义事件
// this.$off(['defineMyEvent', 'demoEvent'])  //等价于this.$off()
 
//解绑所有自定义的事件
this.$off()

注意:  

  1. 如果 vm 被销毁,则其所有子组件、事件均失效
  2. 注意:销毁后的组件的自定义事件也将失效

示例一:

App.vue

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<template>
  <div class="app">
    <!-- <img src="./assets/logo.png"> -->
    <h2>{{msg}}</h2>
    <hr>
    <!-- 第一种写法:
      使用v-on: 或@
      通过父组件给子组件绑定一个自定义事件实现:子给父传递数据
    -->
    <!-- <Student v-on:defineMyEvent="getStudentName"></Student> -->
    <!-- <Student @defineMyEvent.once="getStudentName"></Student> -->
    <Student @defineMyEvent="getStudentName"
             @demoEvent="demoMethod"></Student>
    <hr>
 
    <!-- 第二种写法
      通过父组件给子组件绑定一个自定义事件实现--子给父传递数据,使用ref
     -->
    <Student ref="student" />
 
    <hr>
    <!-- 通过父组件给子组件传递函数类型的props实现:子给父传递数据 -->
    <School :getShcoolName="getShcoolName"></School>
 
  </div>
</template>
 
<script>
// 引入组件
import Student from './components/Student.vue';
import School from './components/School.vue';
export default {
  name: 'App',
  components: {
    Student,
    School
  },
  data () {
    return {
      msg: 'Vue你好'
    }
  },
  methods: {
    getShcoolName (name) {
      console.log('app收到了学校名称:', name)
    },
    getStudentName (name) {
      console.log('App接收到了学生姓名:', name)
    },
    demoMethod () {
      console.log('demoEvent被触发了')
    }
  },
  // 挂载完毕
  mounted () {
    // this.$refs.student.$on('defineMyEvent', this.getStudentName) //绑定自定义事件
 
    this.$refs.student.$once('defineMyEvent', this.getStudentName) //绑定自定义事件 --一次性哦
 
    // 延迟执行
    // // setTimeout(() => {
    // //   this.$refs.student.$on('defineMyEvent', this.getStudentName)
    // // }, 3000)
  }
}
</script>
 
<style scoped>
.app {
  background-color: rgb(178, 168, 168);
}
</style>

Student.vue

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!-- 组件的结构 -->
<template>
  <div class="student">
    <h3>学生姓名:{{name}}</h3>
    <h3>学生性别:{{ age }}</h3>
    <h3>当前求和为:{{mynum}}</h3>
    <button @click="mynum++">++ mynum++</button>
    <button @click="numberAdd">method mynum++</button>
    <button @click="sendStudentName">把学生姓名给app</button>
    <button @click="unbind">解绑自定义事件</button>
    <button @click="destoryVc">销毁当前Student组件的实例vc</button>
  </div>
</template>
 
<!-- 组件交互相关的代码(数据、方法等) -->
<script>
export default ({
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'Student',
  data () {
    return {
      msg: '我正在学习 Vue',
      name: '心仪',
      age: 6,
      mynum: 10
    }
  },
  methods: {
    numberAdd () {
      this.mynum++;
      console.log('numberAdd被执行了')
    },
    sendStudentName () {
      // 触发Student组件实例身上的自定义事件
      this.$emit('defineMyEvent', this.name)
      this.$emit('demoEvent')
    },
    unbind () {
      // this.$off('defineMyEvent')//解绑一个自定义事件
 
      //解绑多个自定义事件
      // this.$off(['defineMyEvent', 'demoEvent'])  //等价于this.$off()
 
      //解绑所有自定义的事件
      this.$off()
    },
    destoryVc () {
      // 销毁了当前Student组件的实例对象
      // 注意:销毁后的组件的自定义事件也将失效
      this.$destroy()
    },
  }
 
})
</script>
 
<!-- 组件的默认样式 css写法 -->
<!-- <style scoped>
.demo {
  background-color: cadetblue;
}
</style> -->
 
<style lang="less" scoped>
.student {
  background-color: cadetblue;
  .myfontsize {
    font-size: 40px;
  }
}
</style>

main.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 引入Vue
import Vue from 'vue'
// 引入App
import App from './App.vue'
// 配置提示
Vue.config.productionTip = false
 
new Vue({
  render: h => h(App),
  /*
  钩子
  如果 vm 被销毁,则其所有子组件、事件均失效
  mounted () {
    setTimeout(() => {
      this.$destroy
    }, 5000)
  }, */
}).$mount('#app')

  

posted on   sunwugang  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示