• vue__组件自定义事件绑定、解绑、触发自定义事件、父子组件之间通信


    目录

    总结:

       组件的自定义事件

           1.一种组件间通信的方式,

           2.使用场景:

           3.绑定自定义事件:

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

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

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

         7.注意:通过this.$refs.xxx.$on('atguigu,回调)

    一、组件自定义事件绑定

           方式一

           方式二

    结果:  方式一和方式二的最终结果是一个样子的

    二、组件自定义事件解绑


    总结:

       组件的自定义事件


           1.一种组件间通信的方式,

                   适用于:子组件===>父组件

           2.使用场景:

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

           3.绑定自定义事件:

               ①第一种方式,

                    在父组件中:

     
               ②第二种方式,在父组件中:
                  
                            .....
                    mounted(){
                    this.$refs.xxx.$on('atguigu',this.test)}

     

     

             ③若想让自定义事件只能触发一次,

                可以使用once修饰符,或$once方法。

    this.$refs.student.$once('atguigu',this.getStudentName)

     

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


       

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


        

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

     

     


         7.注意:通过this.$refs.xxx.$on('atguigu,回调)

          绑定自定义事件时,回调要么配置在methods中,要么用箭头函数,否则this指向会出问题!

     

    一、组件自定义事件绑定

           方式一

               app.vue

    1. <template>
    2. <div class="app">
    3. <h1>{{msg}}h1>
    4. <Student v-on:atguigu="getStudentName">Student>
    5. div>
    6. template>
    7. <script>
    8. // 引入组件
    9. import School from './components/School'
    10. import Student from './components/Student'
    11. export default {
    12. name:'App',
    13. data(){
    14. return{
    15. msg:'你好啊!'
    16. }
    17. },
    18. // 注册组件
    19. components:{
    20. School,
    21. Student,
    22. },
    23. methods:{
    24. // 不论传过来多少个参数,第一个参数赋值给name,剩下的参数在params数组中
    25. getStudentName(name,...params){
    26. console.log('App收到了学生名',name)
    27. }
    28. },
    29. script>
    30. <style scoped>
    31. .app{
    32. background-color:gray ;
    33. padding: 5px;
    34. }
    35. style>

    student.vue

    1. <template>
    2. <div class="student">
    3. <h2 > 学生名称:{{name}}h2>
    4. <h2> 学生年龄:{{age}}h2>
    5. <h2> 学生性别:{{sex}}h2>
    6. <button @click="sendStudentName">把学生的名字给Appbutton>
    7. div>
    8. template>
    9. <script>
    10. export default {
    11. name:'Student',
    12. data(){
    13. return{
    14. name:'张三',
    15. sex:'男',
    16. age:18
    17. }
    18. },
    19. methods:{
    20. // 触发Student组件身上的atguigu事件
    21. sendStudentName(){
    22. // 这里的this指向vc
    23. // emit 爆发的含义
    24. // 注意!!这个地方要写事件名和传入的参数
    25. this.$emit('atguigu',this.name)
    26. }
    27. }
    28. }
    29. script>
    30. <style scoped>
    31. .student{
    32. background-color: red;
    33. padding:5px;
    34. margin-top:30px;
    35. }
    36. style>

           方式二

    1. <template>
    2. <div class="app">
    3. <h1>{{msg}}h1>
    4. <Student ref="student"/>
    5. div>
    6. template>
    7. <script>
    8. // 引入组件
    9. import School from './components/School'
    10. import Student from './components/Student'
    11. export default {
    12. name:'App',
    13. data(){
    14. return{
    15. msg:'你好啊!'
    16. }
    17. },
    18. // 注册组件
    19. components:{
    20. School,
    21. Student,
    22. },
    23. methods:{
    24. // 不论传过来多少个参数,第一个参数赋值给name,剩下的参数在params数组中
    25. getStudentName(name,...params){
    26. console.log('App收到了学生名',name)
    27. }
    28. },
    29. // 挂载完毕
    30. mounted(){
    31. // this.$refs.student获取真实的DOM元素
    32. // $on可以理解为当....时候 $on('atguigu')当atguigu事件被触发的时候
    33. this.$refs.student.$on('atguigu',this.getStudentName)
    34. // 下面这个只能触发一次
    35. // this.$refs.student.$once('atguigu',this.getStudentName)
    36. }
    37. }
    38. script>
    39. <style scoped>
    40. .app{
    41. background-color:gray ;
    42. padding: 5px;
    43. }
    44. style>
    1. <template>
    2. <div class="student">
    3. <h2 > 学生名称:{{name}}h2>
    4. <h2> 学生年龄:{{age}}h2>
    5. <h2> 学生性别:{{sex}}h2>
    6. <button @click="sendStudentName">把学生的名字给Appbutton>
    7. div>
    8. template>
    9. <script>
    10. export default {
    11. name:'Student',
    12. data(){
    13. return{
    14. name:'张三',
    15. sex:'男',
    16. age:18
    17. }
    18. },
    19. methods:{
    20. // 触发Student组件身上的atguigu事件
    21. sendStudentName(){
    22. // 这里的this指向vc
    23. // emit 爆发的含义
    24. // 注意!!这个地方要写事件名和传入的参数
    25. this.$emit('atguigu',this.name)
    26. }
    27. }
    28. }
    29. script>
    30. <style scoped>
    31. .student{
    32. background-color: red;
    33. padding:5px;
    34. margin-top:30px;
    35. }
    36. style>

     

    结果:  方式一和方式二的最终结果是一个样子的

    二、组件自定义事件解绑

    1. <template>
    2. <div class="student">
    3. <h2 > 学生名称:{{name}}h2>
    4. <h2> 学生年龄:{{age}}h2>
    5. <h2> 学生性别:{{sex}}h2>
    6. <button @click="sendStudentName">把学生的名字给Appbutton>
    7. <button @click="unbind">解绑atguigu事件button>
    8. div>
    9. template>
    10. <script>
    11. export default {
    12. name:'Student',
    13. data(){
    14. return{
    15. name:'张三',
    16. sex:'男',
    17. age:18
    18. }
    19. },
    20. methods:{
    21. // 触发Student组件身上的atguigu事件
    22. sendStudentName(){
    23. // 这里的this指向vc
    24. // emit 爆发的含义
    25. // 注意!!这个地方要写事件名和传入的参数
    26. this.$emit('atguigu',this.name)
    27. },
    28. unbind(){
    29. // 这个语句只能解绑一个自定义事件
    30. this.$off('atguigu')
    31. // this.$off(['atguigu','demo']) 解绑多个事件,有几个事件,就往里面写几个事件
    32. // this.$off() 解绑所有的自定义事件
    33. }
    34. }
    35. }
    36. script>
    37. <style scoped>
    38. .student{
    39. background-color: red;
    40. padding:5px;
    41. margin-top:30px;
    42. }
    43. style>

     

     当我们解绑后,无论点多少次“把学生的名字给App”都不会管用

  • 相关阅读:
    从多表连接视图对比人大金仓和Oracle
    【45-线程的实现方式-线程池的创建方式-线程池的执行顺序-CompletableFutrue异步处理】
    5分钟彻底搞懂this指向问题 (附练习题)
    新课程标准培养学生“高考物理关键能力”的实践研究课题文献综述
    剑指Offer || 041.数据流中的移动平均值
    【numpy】np.random
    ios16充电卡在80%充不上去了?可以试试这几种办法
    19c-srvctl注册数据库
    CLion 2023:专注于C和C++编程的智能IDE mac/win版
    react原理篇:组件性能优化(减轻state、使用纯组件PureComponent避免不必要的重新渲染)
  • 原文地址:https://blog.csdn.net/weixin_51351637/article/details/126821077