• Vue2进阶篇-组件间通信的6钟方式



    一、props

    • 使用场景:父子组件通信

    • 注意:

      • 父给子传递:传递的是数据。
      • 子给父传递:传递的是函数,由子组件调用函数传参。
    • 书写方式:

      1. props:['params']
      2. props:{name:String}
      3.  	props:{
         	    name:{
         	    type:String, //类型
         	    required:true, //必要性
         	    default:'老王' //默认值
         	    }
         	}
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7

    二、自定义事件

    • 使用场景:子组件给父组件通信

    • 书写方式:

      1. 在父组件中:
      2. 在父组件中: + this.$refs.demo.$on('fun', function() {})
      3. 若想让自定义事件只能触发一次,可以使用once修饰符,或$once方法。

    三、全局事件总线

    1. 使用场景:万能。

    2. 安装全局事件总线:

      new Vue({
          ......
          beforeCreate() {
              Vue.prototype.$bus = this //安装全局事件总线,$bus就是当前应用的vm
          },
          ......
      }) 
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    3. 使用事件总线:

      1. 接收数据:A组件想接收数据,则在A组件中给$bus绑定自定义事件,事件的回调留在A组件自身。

        methods(){
          demo(data){......}
        }
        ......
        mounted() {
          this.$bus.$on('xxxx',this.demo)
        }
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
      2. 提供数据:this.$bus.$emit('xxxx',数据)

    4. 最好在beforeDestroy钩子中,用$off去解绑当前组件所用到的事件。

    四、pubsub-js(发布与订阅)

    1. 使用场景:万能 (不推荐使用,因为需要额外装插件)

    2. 安装pubsub:npm i pubsub-js

    3. 引入: import pubsub from 'pubsub-js'

    4. 接收数据:A组件想接收数据,则在A组件中订阅消息,订阅的回调留在A组件自身。

      methods(){
        demo(data){......}
      }
      ......
      mounted() {
        this.pid = pubsub.subscribe('xxx',this.demo) //订阅消息
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    5. 提供数据:pubsub.publish('xxx',数据)

    6. 最好在beforeDestroy钩子中,用PubSub.unsubscribe(pid)取消订阅。

    五、Vuex

    1. 使用场景:万能
    2. 使用方法:从0开始学习Vuex

    六、slot插槽

    1. 使用场景:父子组件通信

    2. 分类:默认插槽、具名插槽、作用域插槽

    3. 使用方式:

      1. 默认插槽:

        父组件中:
                <Category>
                   <div>html结构1</div>
                </Category>
        子组件中:
                <template>
                    <div>
                       <!-- 定义插槽 -->
                       <slot>插槽默认内容...</slot>
                    </div>
                </template>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
      2. 具名插槽:

        父组件中:
                <Category>
                    <template slot="center">
                      <div>html结构1</div>
                    </template>
        
                    <template v-slot:footer>
                       <div>html结构2</div>
                    </template>
                </Category>
        子组件中:
                <template>
                    <div>
                       <!-- 定义插槽 -->
                       <slot name="center">插槽默认内容...</slot>
                       <slot name="footer">插槽默认内容...</slot>
                    </div>
                </template>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
      3. 作用域插槽:

        1. 理解:数据在组件的自身,但根据数据生成的结构需要组件的使用者来决定。(games数据在Category组件中,但使用数据所遍历出来的结构由App组件决定)

        2. 具体编码:

          父组件中:
          		<Category>
          			<template scope="scopeData">
          				<!-- 生成的是ul列表 -->
          				<ul>
          					<li v-for="g in scopeData.games" :key="g">{{g}}</li>
          				</ul>
          			</template>
          		</Category>
          
          		<Category>
          			<template slot-scope="scopeData">
          				<!-- 生成的是h4标题 -->
          				<h4 v-for="g in scopeData.games" :key="g">{{g}}</h4>
          			</template>
          		</Category>
          子组件中:
                  <template>
                      <div>
                          <slot :games="games"></slot>
                      </div>
                  </template>
          		
                  <script>
                      export default {
                          name:'Category',
                          props:['title'],
                          //数据在子组件自身
                          data() {
                              return {
                                  games:['红色警戒','穿越火线','劲舞团','超级玛丽']
                              }
                          },
                      }
                  </script>
          
          • 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
  • 相关阅读:
    人工神经网络优化算法,人工智能神经网络模型
    让你的对象变得拗口:JSON.stringify(),我把对象夹进了 JSON 魔法帽!
    centos7.6安装python3.8
    基于Java Web的汽车租赁系统的设计与实现
    商城分销机制怎么做?分销机制比例如何设定?
    Cannot resolve plugin org.springframework.bootspring-boot-maven-plugin「unknown」
    [深度学习]yolov10+deepsort+pyqt5实现目标追踪
    Origin绘制彩色光谱图
    GitHub:ViT-pytorch相关学习-视觉分类方向-1
    定义一个交通工具(Vehicle)的类
  • 原文地址:https://blog.csdn.net/qq_33399435/article/details/125803287