当前位置:   article > 正文

Vue-组件自定义事件_this.refs.name.$on

this.refs.name.$on

父组件使用props传递数据给子组件,子组件给父组件通信除了通过父组件提前给子组件传递一个函数,子组件使用该函数,利用传递参数的形式给父组件传值,回调在父组件中,

这里再提供一种方法:Vue的自定义事件,本文将详细介绍Vue自定义事件

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

绑定自定义事件的方式

                                                                ❤️  第一种方式 ❤️

在父组件中:

<Student  @atguigu ='test' />   或  <Student  v-on:atguigu = 'test'>

methods:{ 

       //除了接受第一个参数,其他参数都放在数组里面

       test(name, ...params) {.....}  

}

                                                                ❤️  第一种方式 ❤️

在父组件中:

<Student ref ='demo'  />  

methods:{ 

       //除了接受第一个参数,其他参数都放在数组里面

       test(name, ...params) {.....}  

}

mounted() {  

       this.$refs.demo.$on('atguigu',this.test)  

} 

 子组件触发自定义事件

 this.$emit('atguigu',this.name, this.sex, 800)

// 子组件触发自定义事件的时候可以传递多个参数

解绑自定义事件

❤️   解绑一个自定义事件:this.$off('atguigu')

❤️   解绑多个自定义事件:this.$off(['atguigu', 'demo'])

❤️   解绑所有的自定义事件:this.$off()

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

<Student ref = "student" @click.native="show">

注意事项:

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

完整案例

App.vue父组件

  1. <template>
  2. <div class="bgq">
  3. <h1>{{ atguigu }}-{{studentname}}</h1>
  4. <!-- 通过父组件给子组件绑定一个自定义事件实现:子给父专递数据(第一种写法使用@)添加once的话就是只触发一次@atguigu.once -->
  5. <!-- <Student @atguigu="getStudentName" @demo="m1" /> -->
  6. <!-- 通过父组件给子组件绑定一个自定义事件实现:子给父专递数据(第二种写法使用ref) -->
  7. <Student ref="student" @click.native="show" />
  8. </div>
  9. </template>
  10. <script>
  11. import Student from "./components/Student.vue";
  12. export default {
  13. name: "App",
  14. components: { Student },
  15. data() {
  16. return {
  17. atguigu: "你好啊",
  18. studentname: "",
  19. };
  20. },
  21. methods: {
  22. getStudentName(name, ...params) {
  23. //除了接受第一个参数,其他参数都放在数组里面
  24. console.log("收到了学生的名字", name, params);
  25. this.studentname = name;
  26. },
  27. m1() {
  28. console.log("demo被触发了");
  29. },
  30. show(){
  31. alert('我是被绑定在组件上面的原生的DOM事件')
  32. }
  33. },
  34. mounted() {
  35. //绑定自定义事件
  36. //3秒后出发
  37. // setTimeout(() => {
  38. // this.$refs.student.$on("atguigu", this.getStudentName);
  39. // }, 3000);
  40. //正常触发
  41. this.$refs.student.$on("atguigu", this.getStudentName);
  42. //只触发一次
  43. // this.$refs.student.$once("atguigu", this.getStudentName);
  44. },
  45. };
  46. </script>
  47. <style scoped>
  48. .bgq {
  49. background-color: skyblue;
  50. padding: 5px;
  51. }
  52. </style>

Student.vue子组件

  1. <template>
  2. <div class="bg">
  3. <h2>学生名称:{{ name }}</h2>
  4. <h2>学生性别:{{ sex }}</h2>
  5. <h2>number: {{ number }}</h2>
  6. <button @click="add">点我number子加</button>
  7. <button @click="sendStudentName">点我获得学生的姓名</button>
  8. <button @click="unbind">解绑atguigu事件</button>
  9. <button @click="dath">点我摧毁Student组件的饿实例对象vc</button>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. name: "Student",
  15. data() {
  16. return {
  17. name: "张三",
  18. sex: "男",
  19. number: 0,
  20. };
  21. },
  22. methods: {
  23. //原生dom事件增加
  24. add() {
  25. console.log(
  26. "我是原生DOM事件,即使Student组件被摧毁了,我还是会调用,但是页面不会输出"
  27. );
  28. this.number++;
  29. },
  30. sendStudentName() {
  31. //触发student组件实例身上的auguigu事件 传递多个参数
  32. this.$emit("atguigu", this.name, this.sex, 800);
  33. //触发damo事件
  34. this.$emit("demo");
  35. },
  36. unbind() {
  37. //解绑atguigu事件,只能解绑一个自定义事件
  38. // this.$off('atguigu')
  39. //解绑多个自定义事件
  40. this.$off(["atguigu", "demo"]);
  41. //解绑所有的自定义事件
  42. // this.$off();
  43. },
  44. dath() {
  45. //摧毁了stedent的实例对象vc,摧毁后所有的Student身上的自定义事件不起效果
  46. this.$destroy();
  47. },
  48. },
  49. };
  50. </script>
  51. <style scoped>
  52. .bg {
  53. background-color: plum;
  54. padding: 5px;
  55. margin-top: 10px;
  56. }
  57. </style>

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号